双向数据绑定,Silverlight和自定义控件/依赖属性

时间:2010-10-25 15:47:32

标签: c# silverlight

如果我在自定义控件上创建一个依赖属性,该属性需要IEnumerable<T>

例如IEnumerable<string>

public static readonly DependencyProperty MyCollectionProperty =
    DependencyProperty.Register("MyCollection", typeof(IEnumerable<string>), typeof(MyControl), new PropertyMetadata(new List<string>()));

public IEnumerable<string> MyCollection
{
     get { return (IEnumerable<string>)GetValue(MyCollectionProperty); }
     set { SetValue(MyCollectionProperty, value); }
}

如果我在这种情况下为ObservableCollection<T><string>数据绑定。 Silverlight会为我处理双向数据绑定吗?

1 个答案:

答案 0 :(得分:0)

MSDN“特别是,如果您正在使用OneWay或TwoWay(例如,您希望在源属性动态更改时更新UI),则必须实现适当的属性更改通知机制,例如INotifyPropertyChanged接口“

ObservableCollection<T>为您实现了INotifyPropertyChanged。 IEnumerable<T>没有。如果您想要简单的双向绑定,只需绑定到ObservableCollection<T>,然后将UpdateSourceTrigger更改为PropertyChanged即可。 XAML:

<ItemSource = "{Binding Path=MyCollection, UpdateSourceTrigger=PropertyChanged}">
相关问题