开发者

Two-way databinding, Silverlight and custom control / dependancy property

If I create an dependency property on a custom control that takes an IEnumerable<T>.

For example with an 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); }
}

If I databind an ObservableCollection<T> or <string> in this case to it. Does Silverlight take care of the two-way databinding for me?


From MSDN "In particular, if you are using OneWay or TwoWay (for example, you want your UI to update when the source properties change dynamically), you must implement a suitable property changed notification mechanism such as the INotifyPropertyChanged interface"

ObservableCollection<T> implements INotifyPropertyChanged for you. IEnumerable<T> does not. If you want easy two way binding simply bind to an ObservableCollection<T>, and change your UpdateSourceTrigger to PropertyChanged. XAML:

<ItemSource = "{Binding Path=MyCollection, UpdateSourceTrigger=PropertyChanged}">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜