C# WPF ObservableCollection CollectionView.CurrentChanged未触发
我的一个C# WPF ObservableCollection CollectionView.CurrentChanged未触发,c#,wpf,.net-3.5,observablecollection,icollectionview,C#,Wpf,.net 3.5,Observablecollection,Icollectionview,我的一个ICollectionViews有问题。ICollectionView的CurrentChanged事件未触发 请看下面我的代码 XAML: 和C#(视图模型) #区域属性 公众可观察的集合企业家 { 获取{return} 设置{u abEnterprise List=value;} } 公共可观测收集区列表 { 获取{return\u abZonesList;} 设置 { _abZonesList=值; } } #端区 私有void InitCollections() {
ICollectionView
s有问题。ICollectionView
的CurrentChanged
事件未触发
请看下面我的代码
XAML:
和C#(视图模型)
#区域属性
公众可观察的集合企业家
{
获取{return}
设置{u abEnterprise List=value;}
}
公共可观测收集区列表
{
获取{return\u abZonesList;}
设置
{
_abZonesList=值;
}
}
#端区
私有void InitCollections()
{
_collectionViewEnterprise=CollectionViewSource.GetDefaultView(EnterpriseList);
_collectionViewZone=CollectionViewSource.GetDefaultView(ZonesList);
//替换
如果(_collectionViewEnterprise==null)
抛出新的NullReferenceException(“Enterprise collectionView为null”);
如果(_collectionViewZone==null)
抛出新的NullReferenceException(“Zone collectionView为null”);
_collectionViewEnterprise.CurrentChanged+=新事件处理程序(OnCollectionViewEnterpriseCurrentChanged);
_collectionViewZone.CurrentChanged+=新事件处理程序(OnCollectionViewZoneCurrentChanged);
}
请帮忙。提前感谢。您需要将组合框数据绑定到collectionview,而不是基础集合。以下是一个工作示例: XAML:
代码隐藏:
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;
namespace CurrentChangedTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
_zoneList.Add(new KeyValuePair<int, string>(0, "zone 0"));
_zoneList.Add(new KeyValuePair<int, string>(1, "zone 1"));
_zoneList.Add(new KeyValuePair<int, string>(2, "zone 2"));
ZoneCollView = new CollectionView(_zoneList);
ZoneCollView.CurrentChanged +=
delegate { Debug.WriteLine(ZoneCollView.CurrentItem); };
DataContext = this;
}
public ICollectionView ZoneCollView { get; set; }
private List<KeyValuePair<int, string>> _zoneList = new List<KeyValuePair<int, string>>();
}
}
使用System.Collections.Generic;
使用系统组件模型;
使用系统诊断;
使用System.Windows;
使用System.Windows.Data;
名称空间CurrentChangedTest
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
_添加(新的键值对(0,“0区”);
_添加(新的KeyValuePair(1,“区域1”);
_添加(新的KeyValuePair(2,“区域2”);
ZoneCollView=新集合视图(_zoneList);
ZoneCollView.CurrentChanged+=
委托{Debug.WriteLine(ZoneCollView.CurrentItem);};
DataContext=this;
}
public ICollectionView ZoneCollView{get;set;}
私有列表_zoneList=新列表();
}
}
#region Properties
public ObservableCollection<GenericNameValuePair<int, string>> EnterpriseList
{
get { return _abEnterpriseList; }
set { _abEnterpriseList = value; }
}
public ObservableCollection<GenericNameValuePair<int, string>> ZonesList
{
get { return _abZonesList; }
set
{
_abZonesList = value;
}
}
#endregion
private void InitCollections()
{
_collectionViewEnterprise = CollectionViewSource.GetDefaultView(EnterpriseList);
_collectionViewZone = CollectionViewSource.GetDefaultView(ZonesList);
//Replace
if(_collectionViewEnterprise == null)
throw new NullReferenceException("Enterprise collectionView is null.");
if (_collectionViewZone == null)
throw new NullReferenceException("Zone collectionView is null.");
_collectionViewEnterprise.CurrentChanged += new EventHandler(OnCollectionViewEnterpriseCurrentChanged);
_collectionViewZone.CurrentChanged += new EventHandler(OnCollectionViewZoneCurrentChanged);
}
<Window x:Class="CurrentChangedTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<StackPanel>
<ComboBox
ItemsSource="{Binding Path=ZoneCollView}"
DisplayMemberPath="Value"
SelectedIndex="0"
IsSynchronizedWithCurrentItem="True" />
<TextBlock Text="{Binding Path=ZoneCollView.CurrentItem}" />
</StackPanel>
</Window>
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;
namespace CurrentChangedTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
_zoneList.Add(new KeyValuePair<int, string>(0, "zone 0"));
_zoneList.Add(new KeyValuePair<int, string>(1, "zone 1"));
_zoneList.Add(new KeyValuePair<int, string>(2, "zone 2"));
ZoneCollView = new CollectionView(_zoneList);
ZoneCollView.CurrentChanged +=
delegate { Debug.WriteLine(ZoneCollView.CurrentItem); };
DataContext = this;
}
public ICollectionView ZoneCollView { get; set; }
private List<KeyValuePair<int, string>> _zoneList = new List<KeyValuePair<int, string>>();
}
}