C# 扩展MVVM的现有控件
我正试图扩展一个名为PivotViewer的现有microsoft控件 此控件有一个现有属性,我想将该属性公开给我的ViewModelC# 扩展MVVM的现有控件,c#,.net,data-binding,mvvm,dependency-properties,C#,.net,Data Binding,Mvvm,Dependency Properties,我正试图扩展一个名为PivotViewer的现有microsoft控件 此控件有一个现有属性,我想将该属性公开给我的ViewModel public ICollection<string> InScopeItemIds { get; } public ICollection inscopeItemId{get;} 我已经创建了一个名为CustomPivotViewer的继承类,我想创建一个可以绑定的依赖属性,该属性将公开基类中InscopeItemId中的值 我花了很长一段时间阅
public ICollection<string> InScopeItemIds { get; }
public ICollection inscopeItemId{get;}
我已经创建了一个名为CustomPivotViewer的继承类,我想创建一个可以绑定的依赖属性,该属性将公开基类中InscopeItemId中的值
我花了很长一段时间阅读有关依赖财产的文章,我变得相当沮丧
这可能吗?编辑:
我意识到了误解,这里有一个新版本(在原问题的背景下):
因此,您可以使用绑定所需的属性,并考虑以下情况:
- 由于此属性是只读的,您将无法将其用于双向绑定
- 只要包含类型未实现INotifyPropertyChanged,则用于显示数据的目标控件将不会收到有关属性值更改的通知
- 只要此属性值返回的值未实现INotifyCollectionChanged(一个示例是ObservableCollection),则对集合的更改不会影响用于显示它的目标控件
- 由于此属性是只读的,您将无法将其用于双向绑定
- 只要包含类型未实现INotifyPropertyChanged,则用于显示数据的目标控件将不会收到有关属性值更改的通知
- 只要此属性值返回的值未实现INotifyCollectionChanged(一个示例是ObservableCollection),则对集合的更改不会影响用于显示它的目标控件
- 编辑:
我意识到了误解,这里有一个新版本(在原问题的背景下):
因此,您可以使用绑定所需的属性,并考虑以下情况:
DependencyProperty
就可以了,这意味着:如果您希望控件中有一个MyBindableProperty
属性,您可以使用该属性执行以下操作:
MyBindableProperty={Binding SomeProperty}
但是,如果希望其他依赖属性
将绑定到它,则可以使用任何属性(无论是依赖属性
还是普通属性)
我不确定您真正需要什么,也许您可以进一步澄清,但如果这是您想要实现的第一个场景,您可以按如下方式执行:
- 创建一个
,我们称之为dependencProperty
,如下所示:BindableInScopeItemIds
/// <summary> /// BindableInScopeItemIds Dependency Property /// </summary> public static readonly DependencyProperty BindableInScopeItemIdsProperty = DependencyProperty.Register("BindableInScopeItemIds", typeof(ICollection<string>), typeof(CustomPivotViewer), new PropertyMetadata(null, new PropertyChangedCallback(OnBindableInScopeItemIdsChanged))); /// <summary> /// Gets or sets the BindableInScopeItemIds property. This dependency property /// indicates .... /// </summary> public ICollection<string> BindableInScopeItemIds { get { return (ICollection<string>)GetValue(BindableInScopeItemIdsProperty); } set { SetValue(BindableInScopeItemIdsProperty, value); } } /// <summary> /// Handles changes to the BindableInScopeItemIds property. /// </summary> private static void OnBindableInScopeItemIdsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var target = (CustomPivotViewer)d; ICollection<string> oldBindableInScopeItemIds = (ICollection<string>)e.OldValue; ICollection<string> newBindableInScopeItemIds = target.BindableInScopeItemIds; target.OnBindableInScopeItemIdsChanged(oldBindableInScopeItemIds, newBindableInScopeItemIds); } /// <summary> /// Provides derived classes an opportunity to handle changes to the BindableInScopeItemIds property. /// </summary> protected virtual void OnBindableInScopeItemIdsChanged(ICollection<string> oldBindableInScopeItemIds, ICollection<string> newBindableInScopeItemIds) { }
希望这有帮助:)您只需要一个
就可以了,这意味着:如果您想在控件中拥有一个DependencyProperty
属性,您就可以使用它执行以下操作:MyBindableProperty
但是,如果希望其他MyBindableProperty={Binding SomeProperty}
将绑定到它,则可以使用任何属性(无论是依赖属性
还是普通属性) 我不确定您真正需要什么,也许您可以进一步澄清,但如果这是您想要实现的第一个场景,您可以按如下方式执行:依赖属性
- 创建一个
,我们称之为dependencProperty
,如下所示:BindableInScopeItemIds
/// <summary> /// BindableInScopeItemIds Dependency Property /// </summary> public static readonly DependencyProperty BindableInScopeItemIdsProperty = DependencyProperty.Register("BindableInScopeItemIds", typeof(ICollection<string>), typeof(CustomPivotViewer), new PropertyMetadata(null, new PropertyChangedCallback(OnBindableInScopeItemIdsChanged))); /// <summary> /// Gets or sets the BindableInScopeItemIds property. This dependency property /// indicates .... /// </summary> public ICollection<string> BindableInScopeItemIds { get { return (ICollection<string>)GetValue(BindableInScopeItemIdsProperty); } set { SetValue(BindableInScopeItemIdsProperty, value); } } /// <summary> /// Handles changes to the BindableInScopeItemIds property. /// </summary> private static void OnBindableInScopeItemIdsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var target = (CustomPivotViewer)d; ICollection<string> oldBindableInScopeItemIds = (ICollection<string>)e.OldValue; ICollection<string> newBindableInScopeItemIds = target.BindableInScopeItemIds; target.OnBindableInScopeItemIdsChanged(oldBindableInScopeItemIds, newBindableInScopeItemIds); } /// <summary> /// Provides derived classes an opportunity to handle changes to the BindableInScopeItemIds property. /// </summary> protected virtual void OnBindableInScopeItemIdsChanged(ICollection<string> oldBindableInScopeItemIds, ICollection<string> newBindableInScopeItemIds) { }
希望这有帮助:)问题中的属性没有setter。这意味着该值会使用某些特定于类的内部代码进行更改,而您不能更改这些代码,对吗?在这种情况下,当属性更改时,您无法看到目标控件上的更改。@Tengiz,提醒我,单向绑定已设置,但未获取,对吗?单向绑定表示源属性显示在目标控件上。因此,您可以显示它,但不能从目标控件更改它。为简单起见:目标可以是文本框,源可以是您的类。同样,如果类未实现INOtifyPropertyChanged,则当类值更改时,控件将不会更新其值。您可以对同时具有setter和getter的任何属性进行双向数据绑定
是指当您希望启用属性本身可绑定(请参见我的回答)时,问题中的属性没有setter。这意味着该值使用某些特定于类的内部代码进行更改,您无法更改,对吗?在这种情况下,当属性更改时,您无法看到目标控件上的更改。@Tengiz,提醒我,单向绑定已设置,但未获取,对吗?单向绑定表示源属性显示在目标控件上。因此,您可以显示它,但不能从目标控件更改它。为简单起见:目标可以是文本框,源可以是您的类。同样,如果类未实现INOtifyPropertyChanged,则当类值更改时,控件将不会更新其值。您可以对同时具有setter和getter的任何属性进行双向数据绑定DependencyProperty
是指当您希望使属性本身可绑定时(请参见我的回答),我认为使用双向绑定已经表明他希望绑定属性。“不是吗?”田吉兹:不是。您可以将DependencyProperty
(正常)属性绑定到具有双向模式的名称
的文本块
(依赖项)属性<你认为这有意义吗?对此textblock的任何更改都不会影响name属性,因此此双向行为与单向行为完全相同。不?是的,它确实影响它。尽管我很确定,但我还是再试了一次,效果很好!那么您有一个类,它有一个受控件绑定影响的非依赖只读属性?我不文本
- 创建一个