2012-11-25 5 views
0

첫 번째 점이 포함 된 그리드를 중심으로 (그리드의 폭/높이를 수동으로 설정하지 않고) 선을 작성해야합니다. 다음은 "Grid1"의 중심에 "Line1"의 한 점이 있으므로 매우 간단한 코드 샘플입니다.WPF 센터 그리드 내용

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="SimpleLine.MainWindow" 
    x:Name="Window" 
    Title="MainWindow" 
    Width="640" Height="480"> 
    <Grid x:Name="LayoutRoot"> 
     <Grid Name="Grid1"> 
      <Line Name="Line1" X2="200" Y2="100" Stroke="Black"></Line> 
     </Grid> 
    </Grid> 
</Window> 

난 뒤에 코드에서 시도 :

Line1.X1 = Grid1.Widht/2; Line1.Y1 = Grid1.Height/2; 

나는 오류 (캐치되지 않는 예외)를 얻을 - "숫자가 아님"X1 (Y1)에 대한 유효한 값이 아닙니다입니다.

아무쪼록 고맙습니다.

추신 : 저는 WPF- 초보자입니다.

답변

1

너비/높이를 명시 적으로 설정하지 않은 경우 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 대해 동일하여 분할한다. 크기가 변경된 경우에도 줄은 가운데에 유지됩니다.