收集依赖性属性

问题描述 投票:0回答:2

ObservableCollection
问题正在添加到
observableCollection
不更新。

我需要完全替换

<MyControl MyCollectionProperty = {Binding MyObservableCollection} ...
以使其正常工作,例如

MyObservableCollection

有一种更好的方法来解决这个问题吗?
Edit:

MyCollectionProperty

在Grantz回答的内容中,我建议使用类型

MyObservableCollection

声明该属性,并在运行时检查集合对象是否实现

MyObservableCollection = null; MyObservableCollection = new ObservableCollection(){...}

接口。这为用作属性值的具体收集实现提供了更大的灵活性。然后,用户可以决定拥有自己的可观察收藏的专业实施。
还注意,在
public ObservableCollection<string> Columns { get { return (ObservableCollection<string>)GetValue(ColumnsProperty); } set { SetValue(ColumnsProperty, value); } } public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(ObservableCollection<string>), typeof(MyControl), new PropertyMetadata(new ObservableCollection<string>(), OnChanged));
c# wpf data-binding observablecollection dependency-properties
2个回答
22
投票
IEnumerable<string>


below是一个可能有帮助的工作示例。

在此示例中,当单击“更改”的添加按钮被写入控制台时,该方法被立即调用。

控制
INotifyCollectionChanged

窗口

ColumnsPropertyChanged
窗后代码

CollectionChanged

6
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.