简体   繁体   English

DependencyProperty的值不会从XAML传播到后面的代码

[英]The value of DependencyProperty is not propagated from XAML to code behind

The problem is that DependencyProperty is not getting assigned to the value specified in XAML: 问题是DependencyProperty没有分配给XAML中指定的值:

I have two different user controls let's say UserControl1 and UserControl2 that define another user control which is common to both controls: 我有两个不同的用户控件,比如说UserControl1和UserControl2,它们定义了两个控件共有的另一个用户控件:

UserControl1.xaml: UserControl1.xaml:

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

UserControl2.xaml UserControl2.xaml

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

The difference between both UserControls is that UserControl1 has to display the data using Horizontal orientation and UserControl2 has to display the data using Vertical Orientation. 两种UserControl的区别在于,UserControl1必须使用“水平方向”显示数据,而UserControl2必须使用“垂直方向”显示数据。

Inside of the MyUserControl's code behind I defined a Dependency Property as follows: 在MyUserControl的代码内部,我定义了一个Dependency属性,如下所示:

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

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

These are fragment of the code inside of MyUserControl.xaml: 这些是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>

The idea is to change ColumnDefinitions to RowDefinitions and Grid.Column to Grid.Rows depending on the orientation flag: 想法是根据方向标记将ColumnDefinitions更改为RowDefinitions,将Grid.Column更改为Grid.Rows:

If the flag is "Horizontal", UserControl displays "DetailsGrid" and "Option Data" Grid side by side, meaning "DetailGrid" is in Column 0 and "Option Data" Grid in Column 1. 如果标记为“ Horizo​​ntal”,则UserControl并排显示“ DetailsGrid”和“ Option Data”网格,这意味着“ DetailGrid”在第0列中,而“ Option Data”网格在第1列中。

If the flag is "Vertical" UserControl displays "DetailGrid" in Row 1 and "Option Data" in Row 0. 如果标记为“ Vertical”,则UserControl在第1行中显示“ DetailGrid”,在第0行中显示“ Option Data”。

Need some help on that issue. 在这个问题上需要一些帮助。

Thank you in advance. 先感谢您。

Two things, make the dependency property have a changed handler and verify in the debugger that the value is actually making it to the user control. 两件事情,使dependency属性的处理程序发生更改,并在调试器中验证该值实际上已在用户控件中使用。 Also setup a default value for the user control (the below uses "Horizontal") but you decide how to handle that. 还要为用户控件设置一个默认值(以下使用“水平”),但是您可以决定如何处理它。

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;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM