- RomanRobot
- Posts: 3
- Joined:
Collection DependencyProperty
What is the intended way to define DependencyProperties that bind to collections? I have a DependencyProperty defined
that I am binding an ObservableCollection<string> called Interactions to like this
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.
Code: Select all
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); }
}
Code: Select all
<InteractionMenu Interactions="{Binding Interactions}"/>
-
sfernandez
Site Admin
- Posts: 2995
- Joined:
Re: Collection DependencyProperty
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:
The text block will show a 3 here meaning that MyBorder.Interactions property was set with the binding value correctly.
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:
Code: Select all
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); }
}
}
}
Code: Select all
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");
}
}
Code: Select all
<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>
Who is online
Users browsing this forum: Xaron and 12 guests