너비/높이를 명시 적으로 설정하지 않은 경우 ActualWidth와 ActualHeight를 사용해야합니다.
Line1.X1 = Grid1.ActualWidth/2; Line1.Y1 = Grid1.ActualHeight/2;
XAML에서 원하는 경우 바인딩 된 값에 대해 논리를 수행하도록 BindingConverters를 만들 수 있습니다.
XAML :
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication6"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:DivideByConverter x:Key="Divider" />
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Line Name="Line1" X2="{Binding ElementName=LayoutRoot, Path=ActualWidth, Converter={StaticResource ResourceKey=Divider}}" Y2="{Binding ElementName=LayoutRoot, Path=ActualHeight, Converter={StaticResource ResourceKey=Divider}}" Stroke="Black"/>
</Grid>
</Window>
바인딩 변환기 :
public class DivideByConverter : IValueConverter
{
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value produced by the binding source.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The xmlentry to the language value</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int divider = 2;
if (value is double)
{
return (double)value/divider;
}
return value;
}
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value that is produced by the binding target.</param>
/// <param name="targetType">The type to convert to.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}
이는 바인더 제본 값 (ActualWidth)을 받아 둘의 ActualHeight 대해 동일하여 분할한다. 크기가 변경된 경우에도 줄은 가운데에 유지됩니다.