如果我在采用 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); }
}
如果我数据绑定(bind) ObservableCollection<T>
或 <string>
在这种情况下。 Silverlight 是否为我处理双向数据绑定(bind)?
最佳答案
来自 MSDN “特别是,如果您正在使用 OneWay 或 TwoWay(例如,您希望在源属性动态更改时更新 UI),则必须实现合适的属性更改通知机制,例如 INotifyPropertyChanged 接口(interface)”
ObservableCollection<T>
为您实现 INotifyPropertyChanged。 IEnumerable<T>
才不是。如果您想要简单的双向绑定(bind),只需绑定(bind)到 ObservableCollection<T>
, 并更改您的 UpdateSourceTrigger
至 PropertyChanged
. XAML:
<ItemSource = "{Binding Path=MyCollection, UpdateSourceTrigger=PropertyChanged}">
关于c# - 双向数据绑定(bind)、Silverlight 和自定义控件/依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4016303/