【问题标题】:The value of DependencyProperty is not propagated from XAML to code behindDependencyProperty 的值不会从 XAML 传播到后面的代码
【发布时间】:2012-11-08 18:48:15
【问题描述】:

问题在于 DependencyProperty 未分配给 XAML 中指定的值:

我有两个不同的用户控件,比如说 UserControl1 和 UserControl2,它们定义了两个控件共有的另一个用户控件:

UserControl1.xaml:

<modulename:MyUserControl ....somecode... DataOrientation="Horizontal"> 
</modulename:MyUserControl>

UserControl2.xaml

<modulename:MyUserControl ....somecode... DataOrientation="Vertical"> 
</modulename:MyUserControl>

两个 UserControl 的区别在于 UserControl1 必须使用水平方向显示数据,而 UserControl2 必须使用垂直方向显示数据。

在 MyUserControl 的代码中,我定义了一个依赖属性,如下所示:

public static readonly DependencyProperty DataOrientationProperty = 
DependencyProperty.Register ("DataOrientation",typeof(String),typeof(MyUserControl));

public String DataOrientation 
{
   get {return (String)GetValue(DataOrientationProperty);}
   set { SetValue(DataOrientationProperty, value); }
}

这些是 MyUserControl.xaml 中的代码片段:

...
<StockPanel>
  <Grid Name="MyGrid" SizeChanged="MyGrid_SizeChanged">
    <Grid.ColumnDefinitions>
      <ColumnDefinitions Width="*"/>
      <ColumnDefinitions Width="100"/>
    </Grid.ColumnDefinitions>

    <ScrollViewer Name="MySV" Grid.Column="0" ....>
      <Grid Name="DetailGrid"/>
    </ScrollViewer>

    <Grid Grid.Column="1" .........>
    ......Some Option Data....
   </Grid>
  </Grid>
 </StockPanel>

想法是根据方向标志将 ColumnDefinitions 更改为 RowDefinitions 并将 Grid.Column 更改为 Grid.Rows:

如果标志为“Horizo​​ntal”,则UserControl并排显示“DetailsGrid”和“Option Data”网格,表示“DetailGrid”在第0列,“Option Data”网格在第1列。

如果标志是“垂直”,用户控件在第 1 行显示“DetailGrid”,在第 0 行显示“选项数据”。

在这个问题上需要一些帮助。

提前谢谢你。

【问题讨论】:

  • 我认为您需要一个DataTrigger,它根据{Binding RelativeSource={RelativeSource Self}, Path=DataOrientation} 的值设置对象的Grid.RowGrid.Column

标签: c# wpf xaml


【解决方案1】:

两件事,让依赖属性有一个改变的处理程序,并在调试器中验证该值实际上是在将它传递给用户控件。还要为用户控件设置一个默认值(下面使用“水平”),但您决定如何处理。

public string DataOrientation
   {
       get { return (string)GetValue(DataOrientationProperty); }
       set { SetValue(DataOrientationProperty, value); }
   }

   /// <summary>
   /// Identifies the DataOrientation dependency property.
   /// </summary>
   public static readonly DependencyProperty DataOrientationProperty =
       DependencyProperty.Register(
           "DataOrientation",
           typeof(string),
           typeof(MyClass),
           new PropertyMetadata("Horizontal",  // Default to Horizontal; Can use string.Empty
                                 OnDataOrientationPropertyChanged));

   /// <summary>
   /// DataOrientationProperty property changed handler.
   /// </summary>
   /// <param name="d">MyClass that changed its DataOrientation.</param>
   /// <param name="e">Event arguments.</param>
   private static void OnDataOrientationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
       MyClass source = d as MyClass;  // Put breakpoint here.
       string value = (string)e.NewValue;
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2022-11-25
    • 1970-01-01
    • 2011-12-19
    • 2020-08-31
    • 1970-01-01
    相关资源
    最近更新 更多