WPF中心网格内容

时间:2012-11-25 23:44:27

标签: c# wpf xaml layout grid

我需要创建一条线,第一个点以其包含的网格为中心(无需手动设置网格的宽度/高度)。这是我想要更新的非常简单的代码示例,所以我在“Grid1”中有一个“Line1”点。

<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)的有效值。

感谢您的任何努力。

PS:我是WPF初学者。

1 个答案:

答案 0 :(得分: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.");
        }
    }

这将采用Binded值(ActualWidth)并将其除以2并对ActualHeight进行相同处理。 因此,即使您的来自调整大小,该线将保留在中心