当前位置: 首页 > 编程语言 > C#

BindingProxy:绑定到索引属性分享

时间:2023-04-11 01:35:59 C#

C#学习教程:BindingProxy:绑定到索引属性我还有一个可以隐藏/显示网格列的上下文菜单。初始加载有效,如果字典“ColumnsVisibility”在InitializeComponent()之前用信息填充,则应用我设置的DictionaryEntry值。我的目标是选中上下文菜单中的复选框,该列出现/消失。我正在使用Proxy,因为ContextMenu和Columns不是与DataGrid或其他任何东西相同的可视化树的成员。我的问题是,选中/取消选中ContextMenu中的CheckBox不会更改ColumnsVisibility[ElementName]的值。如果我向复选框添加一个选中/取消选中事件,我可以在代码中使用它来更改它,但是触发PropertyChanged-Event不会在视觉上改变任何东西。该列保持原样。BindingProxy是否将事件转发到GUI,反之亦然?目前似乎没有。任何人都知道如何解决这个问题?编辑:BindingProxy:publicclassBindingProxy:Freezable{#regionOverridesofFreezableprotectedoverrideFreezableCreateInstanceCore(){returnnewBindingProxy();}#endregionpublicobjectData{get{return(object)GetValue(DataProperty);}set{SetValue(DataProperty,value);}}//使用DependencyProperty作为数据的后备存储。这会启用动画、样式、绑定等...}Edit2:ColumnsVisibilty属性privateDictionary_ColumnsVisibility=newDictionary();公共字典ColumnsVisibility{get{return(_ColumnsVisibility);}set{_ColumnsVisibility(!Property=value;Chiangifnull)PropertyChanged(null,newPropertyChangedEventArgs("ColumnsVisibility"));在InitializeComponent()之前,这是在加载时完成的:_ColumnsVisibility.Add("ElementName",错误的);编辑3好了,这是完整的源代码:交互通讯:usingSystem.Collections.Generic;使用系统安全;使用System.Windows;使用System.Windows.Controls;使用System.Collections.ObjectModel;使用System.ComponentModel;namespaceHyperV{//////HyperVControl.xaml的交互逻辑///publicpartialclassHyperVControl:UserControl,INotifyPropertyChanged{publicHyperVControl(){#region设置列的默认可见性_ColumnsVisibility.Add("ElementName",false);//(...)#endregionInitializeComponent();}#regionControltriggeredprivatevoidUserControl_Loaded(objectsender,RoutedEventArgse){}//////由复选框触发,这些复选框位于DataGrid-Header的上下文菜单中以显示/隐藏列//////发送此命令的复选框///privatevoidCheckBox_Checked(objectsender,RoutedEventArgse){//这将ColumnsVisibility中的值设置为确定。该值不是通过绑定设置的(但应该...)ColumnsVisibility[((CheckBox)sender).Tag.ToString()]=(bool)((CheckBox)sender).IsChecked;//这一切都不起作用if(PropertyChanged!=null){PropertyChanged(null,newPropertyChangedEventArgs("ColumnsVisibility"));PropertyChanged(null,newPropertyChangedEventArgs("ColumnsVisibility[Machinename]"));PropertyChanged(null,newPropertyChangedEventArgs("Data.ColumnsVisibility"));PropertyChanged(null,newPropertyChangedEventArgs("Data.ColumnsVisibility[Machinename]"));}}#endregion#region属性(私有和公共)私有ObservableCollection_HVMachineList;私人字典_ColumnsVisibility=newDictionary();//////包含有关虚拟客户端的所有加载信息///publicObservableCollectionHVMachineList{get{return_HVMachineList;}设置{_HVMachineList=值;如果(PropertyChanged!=null)PropertyChanged(this,newPropertyChangedEventArgs("HVMachineList"));}}//////设置///publicDictionaryColumnsVisibility{get{return(_ColumnsVisibility);}set{_ColumnsVisibility=value;如果(PropertyChanged!=null)PropertyChanged(null,newPropertyChangedEventArgs("ColumnsVisibility"));}}#endregion#regionEvents//更新表单上的内容publiceventPropertyChangedEventHandlerPropertyChanged;#endregion}//BindingProxy#regionFreezableforContext-Menu-Data-Transmition}#endregionpublicobjectData{get{return(object)GetValue(DataProperty);}set{SetValue(DataProperty,value);}}//使用DependencyProperty作为数据的后备存储。这会启用动画、样式、绑定等...}#endregion}XAML:编辑:这一定是一个错误:PropertyChanged(null,newPropertyChangedEventArgs("ColumnsVisibility"));它应该是:PropertyChanged(this,newPropertyChangedEventArgs("ColumnsVisibility"));我已经盲目地将其复制到我的代码中好吧,有时你就是看不到你面前的东西。对于未来,我建议你在一些基类中使用某种函数publicclassNotifyPropertyChangeableBase:INotifyPropertyChanged//通常我在我的项目中将它命名为有点像'ViewModelBase',但你的实际类是控件,所以它不是最适当的名称{protectedvoidOnPropertyChanged(StringpropertyName){if(this.PropertyChanged!=null)this.PropertyChanged(this,newPropertyChangedEventArgs(propertyName));}公共事件PropertyChangedEventHandlerPropertyChanged;它没有解决问题,但至少有一个问题编辑(我希望)最终解决方案:看起来你不能通知WPF引擎你的字典已经改变了它的项目。标准字典本身不执行此操作(它不执行ICollectionChanged甚至INotifyPropertyChanged)。这就是为什么你必须使用自己的字典,或者更精确的封装字典类:publicclassDictionaryNotificationWrapper:IDictionary,INotifyPropertyChanged{#regionFields#endregion#regionConstructorspublicDictionaryNotificationWrapper(IDictionaryinnerDictionary){if(innerDictionary==null)thrownewArgumentNullException("innerDictionary","内部字典为空");this.innerDictionary=innerDictionary;}#endregion#regionIDictionary实现publicTValuethis[TKeykey]{get{returnthis.innerDictionary[key];}set{this.innerDictionary[key]=value;this.OnPropertyChanged("项目[]");this.OnPropertyChanged("计数");}}#endregion#region未实现IDictionary成员-您可以自由完成工作publicvoidAdd(TKeykey,TValuevalue){thrownewNotImplementedException();}publicboolContainsKey(TKeykey){thrownewNotImplementedException();}publicICollectionKeys{get{thrownewNotImplementedExcep化();}}publicboolRemove(TKeykey){thrownewNotImplementedException();}publicboolTryGetValue(TKeykey,outTValuevalue){thrownewNotImplementedException();}publicICollectionValues{get{thrownewNotImplementedException();}}publicvoidAdd(KeyValuePairitem){thrownewNotImplementedException();}publicvoidClear(){thrownewNotImplementedException();}publicboolContains(KeyValuePairitem){thrownewNotImplementedException();}publicvoidCopyTo(KeyValuePair[]array,intarrayIndex){thrownewNotImplementedException();}publicintCount{get{thrownewNotImplementedException();}}publicboolIsReadOnly{get{thrownewNotImplementedException();}}publicboolRemove(KeyValuePairitem){thrownewNotImplementedException();}publicIEnumerator>GetEnumerator(){thrownewNotImplementedException();}System.Collections.IEnumeratorSystem.Collections.IEnumerable.GetEnumerator(){thrownewNotImpleme异常异常();}#endregion#regionINotifyPropertyChanged实现publiceventPropertyChangedEventHandlerPropertyChanged;protectedvoidOnPropertyChanged(StringpropertyName){if(this.PropertyChanged!=null)this.PropertyChanged(this,newPropertyChangedEventArgs(propertyName));}#endregion}有了这样一堂课://////HyperVControl.xaml的交互逻辑///publicpartialclassHyperVControl:UserControl,INotifyPropertyChanged{#regionConstructorspublicHyperVControl(){//表单初始化InitializeComponent();//初始化列可见性集合IDictionaryinnerColumnsVisibilityDictionary=newDictionary();innerColumnsVisibilityDictionary.Add("ElementName",true);//包装可见性字典this.ColumnsVisibility=newDictionaryNotificationWrapper(innerColumnsVisibilityDictionary);//初始化网格的数据源this.HVMachineList=newObservableCollection();this.HVMachineList.Add(新的HyperVMachine());this.HVMachineList.Add(newHyperVMachine());this.HVMachineList.Add(newHyperVMachine());您将能够在没有任何代码的情况下通知您的视觉组件PS:iINotifyProperyChanged已经实现,它通知Item[]索引属性更改,但您可以尝试实现INotifyCollectionChanged接口-我只是不确定它是如何工作的与索引绑定。PPS:我没有看到你的评论,你发现了这个this.PropertyChanged(这个,新的...问题。PPPS:如果你有时间,请将问题标题更改为“BindingProxy:绑定到索引属性”以便更多好好反映问题,只保留最后编辑的代码(避免重复)——算是社区服务以上是C#学习教程:BindingProxy:Bindingtoindexedproperties分享全部内容,如果对大家有用还有我需要了解更多C#学习教程的,希望大家多多关注~本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: