7

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?

Jimmy
  • 3,224
  • 5
  • 29
  • 47

2 Answers2

7

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();
    }
} 
Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
0

You haven't specified the mode of your binding. Perhaps it is defaulting to one-way only? Try: {Binding Path=CheckList, Mode=TwoWay}

Alexander R
  • 2,468
  • 24
  • 28