I tried to set the width and height of my WPF control based on the current window size using data binding and value converters.
This is how I did that.
<Window x:Class=" Blogsprajeesh.Blogspot.WPFSamples.SetControlSizeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:Blogsprajeesh.Blogspot.WPFSamples "
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="mainWindow"
Title="SetControlSizeView" Height="300" Width="575">
<Window.Resources>
<local:HeightConverter x:Key="heightConverter" />
Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
Grid.RowDefinitions>
<TextBlock Background="RoyalBlue"
Height="{Binding ElementName=mainWindow,
Path=ActualHeight,
Converter={StaticResource heightConverter},
ConverterParameter=3}"
Text="Some sample text" Grid.Row="0"
VerticalAlignment="Center" />
Grid>
Window>
IValueConverter implementation
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double __height;
double __paramValue;
if(double.TryParse(value.ToString(), out __height))
{
if (double.TryParse(parameter.ToString(), out __paramValue))
return __height / __paramValue;
else
return __height / 2;
}
return 25;
}