简体   繁体   中英

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:

I have two different user controls let's say UserControl1 and UserControl2 that define another user control which is common to both controls:

UserControl1.xaml:

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

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.

Inside of the MyUserControl's code behind I defined a Dependency Property as follows:

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:

...
<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:

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.

If the flag is "Vertical" UserControl displays "DetailGrid" in Row 1 and "Option Data" in Row 0.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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