RomanRobot
Topic Author
Posts: 3
Joined: 26 Aug 2017, 00:40

Collection DependencyProperty

19 Aug 2018, 02:12

What is the intended way to define DependencyProperties that bind to collections? I have a DependencyProperty defined
public static readonly DependencyProperty InteractionsProperty =
        DependencyProperty.Register(nameof(Interactions), typeof(ObservableCollection<string>), typeof(InteractionMenu),
        new FrameworkPropertyMetadata(new ObservableCollection<string>()));

public ObservableCollection<string> Interactions
{
    get { return GetValue(InteractionsProperty) as ObservableCollection<string>; }
    set { SetValue(InteractionsProperty, value); }
}
that I am binding an ObservableCollection<string> called Interactions to like this
<InteractionMenu Interactions="{Binding Interactions}"/>
but the setter never gets called. I have tried having them be IEnumerable<string>, IEnumerable, IList<string>, string[], ObservableCollection<int>, and it doesn't work no matter what. My other DependencyProperties work but I can't figure out how to make a DependencyProperty of a collection type.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Collection DependencyProperty

22 Aug 2018, 02:14

Hi,

Interactions property getter and setter (as they are only a wrapper for a DependencyProperty) are not used by the binding architecture to get/set values. We internally use DependencyObject.GetValue and SetValue directly.

I set up a test with a control defining your Interactions dependency property and it seems to work fine:
namespace Tests
{
    public class MyBorder : Noesis.Border
    {
        public static readonly Noesis.DependencyProperty InteractionsProperty = Noesis.DependencyProperty.Register(
            "Interactions", typeof(ObservableCollection<string>), typeof(MyBorder),
            new Noesis.FrameworkPropertyMetadata(new ObservableCollection<string>()));

        public ObservableCollection<string> Interactions
        {
            get { return GetValue(InteractionsProperty) as ObservableCollection<string>; }
            set { SetValue(InteractionsProperty, value); }
        }
    }
}
public class ViewModel
{
    public ObservableCollection<string> Interactions { get; private set; }
    public ViewModel()
    {
        Interactions = new ObservableCollection<string>();
        Interactions.Add("first");
        Interactions.Add("second");
        Interactions.Add("third");
    }
}
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Tests">
    <local:MyBorder x:Name="bd" Interactions="{Binding Interactions}"/>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="20" Text="{Binding Interactions.Count, ElementName=bd}"/>
</Grid>
The text block will show a 3 here meaning that MyBorder.Interactions property was set with the binding value correctly.

Who is online

Users browsing this forum: No registered users and 1 guest