Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency property List<string> in UserControl

I have a dependency property(List of string) in a user control in my dot net assembly as below

public partial class ItemSelectionUserControl : UserControl
{
   public List<string> AvailableItems
    {
        get { return (List<string>)this.GetValue(AvailableItemsProperty); }
        set { this.SetValue(AvailableItemsProperty, value); }
    }
    public static readonly DependencyProperty AvailableItemsProperty = DependencyProperty.Register(
      "AvailableItems", typeof(List<string>), typeof(ItemSelectionUserControl), new FrameworkPropertyMetadata{BindsTwoWayByDefault =true});


    public ItemSelectionUserControl()
    {
        InitializeComponent();
    }


}

I am trying to use this usercontrol in another usercontrol in a different assembly as below

    <UserControl 
     xmlns:ctrl="clr-namespace:HH.Windows.UserControls;assembly=HH.Windows.UserControls"
    />

   // .....
    <Grid>
     <ctrl:ItemSelectionUserControl Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="3" AvailableItems="{Binding Path=CheckList}"/>
    </Grid>

I can see the get accessor of CheckList is getting called, but it is not setting the dependency property "AvailableItems". The breakpoint in the set of "AvailableItems" is never getting called. What am I doing wrong?

like image 666
Jimmy Avatar asked Jan 20 '12 15:01

Jimmy


1 Answers

As far as I'm aware, WPF may not call the setter of your property directly if it exposes a DependencyProperty. Instead it can set the DependencyProperty directly. For more information, see Dependency Properties Overview on MSDN. In particular this section:

Dependency Properties Might Be "Set" in Multiple Places
The following is example XAML where the same property (Background) has three different "set" operations that might influence the value ...

To test whether this is occurring in your example (plus get a notification where you can operate on the set value), you can try adding a Callback in the FrameworkPropertyMetadata

e.g.

public partial class ItemSelectionUserControl : UserControl 
{    
    public List<string> AvailableItems     
    {         
        get { return (List<string>)this.GetValue(AvailableItemsProperty); }         
        set { this.SetValue(AvailableItemsProperty, value); }     
    }     

    public static readonly DependencyProperty AvailableItemsProperty = 
        DependencyProperty.Register("AvailableItems", 
        typeof(List<string>), typeof(ItemSelectionUserControl), 
        new FrameworkPropertyMetadata(OnAvailableItemsChanged) 
        {
            BindsTwoWayByDefault =true
        });       

    public ItemSelectionUserControl()     
    {         
        InitializeComponent();     
    }   

    public static void OnAvailableItemsChanged(
           DependencyObject sender, 
           DependencyPropertyChangedEventArgs e)
    {
        // Breakpoint here to see if the new value is being set
        var newValue = e.NewValue;
        Debugger.Break();
    }
} 
like image 78
Dr. Andrew Burnett-Thompson Avatar answered Oct 19 '22 01:10

Dr. Andrew Burnett-Thompson