【问题标题】:WPF DataBinding: Grid ColumnDefinition Width to UserControl DependencyPropertyWPF DataBinding:到 UserControl DependencyProperty 的 Grid ColumnDefinition 宽度
【发布时间】:2010-09-15 02:53:35
【问题描述】:

我不确定我做错了什么,但我正在尝试使用 DataBinding 和 DependencyProperties 将我的用户控件与我的网格的 ColumnWidth 对齐。

我的表单页面上有一个 GridView,其中包含一个用户控件(地址)的两个实例。地址用户控件包含一个文本标签“标题”,以便我可以将用户控件命名为“办公室地址”或“邮寄地址”等,并使用它自己的 GridView 进行格式化。我向名为“HeaderWidth”的用户控件添加了一个 DependencyProperty,它只是一个 Int32。问题是即使有绑定,事情也不会对齐。我不确定我做错了什么。

AddressField.xaml

<UserControl x:Class="AddressField" x:Name="PART_AddressFieldControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         >
 <StackPanel Orientation="Horizontal">
  <Grid x:Name="StandardView">
   <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
   </Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
    <ColumnDefinition Width="{Binding ElementName=PART_AddressFieldControl, Path=HeaderWidth}" />
    <ColumnDefinition Width="1*" />
    <ColumnDefinition Width="Auto" />
    <!-- ETC REMOVED FOR COMPACTNESS -->
   </Grid.ColumnDefinitions>

   <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" Text="{Binding ElementName=PART_AddressFieldControl, Path=Header}" />
   <TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="8" MinWidth="300" Text="{Binding Address1}" />
   <!-- ETC REMOVED FOR COMPACTNESS -->

  </Grid>
 </StackPanel>
</UserControl>

AddressField.cs

public partial class AddressField : UserControl
{
    static AddressField()
    {
        HeaderProperty = DependencyProperty.Register("Header", typeof(String), typeof(AddressField), new UIPropertyMetadata("Address:"));

        HeaderWidthProperty = DependencyProperty.Register("HeaderWidth", typeof(Int32), typeof(AddressField), new UIPropertyMetadata());

        ViewProperty = DependencyProperty.Register("View", typeof(AddressFieldView), typeof(AddressField), new UIPropertyMetadata(AddressFieldView.STANDARD));
    }

    public AddressField()
    {
        InitializeComponent();
    }

    public String Header
    {
        get { return (String)GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }
    public static readonly DependencyProperty HeaderProperty;

    public Int32 HeaderWidth
    {
        get { return (Int32)GetValue(HeaderWidthProperty); }
        set { SetValue(HeaderWidthProperty, value); }
    }
    public static readonly DependencyProperty HeaderWidthProperty;
}

RegistrationForm.xaml

<!-- ETC REMOVED FOR COMPACTNESS -->

<Grid.ColumnDefinitions>
 <ColumnDefinition Width="Auto" Name="FirstWidth" />
 <ColumnDefinition Width="Auto" />
 <!-- ... ETC ... -->
</Grid.ColumnDefinitions>

<vws:AddressField Grid.Row="1" Grid.Column="0" Grid.RowSpan="3" Grid.ColumnSpan="9" Header="Office Address:" DataContext="{Binding OfficeAddressVM}" HeaderWidth="{Binding ElementName=FirstWidth, Path=ActualWidth}" />
<vws:AddressField Grid.Row="4" Grid.Column="0" Grid.RowSpan="3" Grid.ColumnSpan="9" Header="Mailing Address:" DataContext="{Binding MailingAddressVM}" HeaderWidth="{Binding ElementName=FirstWidth, Path=ActualWidth}" />

基本上,我正在尝试将用户控件的文本标签与注册表单的第一列宽度对齐。

编辑: 哦,只是因为我很好奇:我设置了一个调试断点并深入 UI 树并到达了 AddressField 的“标题”(TextBlock),ActualWidth 不是 0.0 而是 73.9 或类似的值,但 UI 没有不考虑这一点,它仍然不可见,因为宽度为 0.0。

【问题讨论】:

    标签: wpf data-binding dependency-properties


    【解决方案1】:

    你知道有更简单的方法:)

    只需将Grid.IsSharedSizeScope="True" 放在主窗口(不在控件中)的网格上,并将SharedSizeGroup="someRandomName" 放在您希望共享宽度的所有列上 - 在这种情况下,只有您在用户中绑定的列控制,即用 SharedSizeGroup 替换绑定。而已!任何具有相同 SharedSizeGroup 值的列现在都将具有相同的大小,即它们中最大的列的大小。

    当然,它只适用于自动调整大小,但听起来这就是你想要的。

    更多信息你可以看herehere

    编辑:至于您做错了什么,我怀疑外部网格列的大小设置为 Auto,但没有任何自己的内容来调整大小(因为所有控件都跨越多个列),宽度为零。然后绑定传播到用户控件。不过那里有很多复杂的交互,而且我没有完整的 XAML 来判断,所以我可能错了。

    【讨论】:

    • 实际上,我最终完全改变了表单的样式,所以我想这不再是问题了。不过,我确实怀疑您的答案可能会奏效。另外,在考虑之后,我怀疑问题只是......我试图从父网格绑定到用户控件,这会触发调整大小......类似于这些方面的东西。
    • 是的,我确实使用您提供的 sn-ps 对其进行了测试,以确保我没有记错某些内容,因此它确实有效,而且这也是我之前成功使用过的一种技术。这是真的,虽然有很多方法可以解决这个问题:)
    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 2011-11-10
    • 2013-01-20
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多