gpt4 book ai didi

c# - WPF ObservableCollection CollectionView.CurrentChanged 未触发

转载 作者:行者123 更新时间:2023-11-30 21:22:32 24 4
gpt4 key购买 nike

我的一个 ICollectionView 有问题。 ICollectionViewCurrentChanged 事件未触发。

请看下面我的代码。

XAML:

            <!-- Publication -->
<TextBlock Name="tbkPublication" Text="{x:Static abConst:Print.tbkPublicationText}" Grid.Row="0" Grid.Column="0" Margin="3" ></TextBlock>
<ComboBox Grid.Column="1" Grid.Row="0" Margin="3"
Name="cmbPublication" BorderThickness="1"
ItemsSource="{Binding Path=EnterpriseList}" DisplayMemberPath="Name"
SelectedValuePath="Value" SelectedIndex="0"
IsSynchronizedWithCurrentItem="True" />

<!-- Distribution Area Region -->
<TextBlock Name="tbkDistributionArea" Text="{x:Static abConst:Print.tbkDistributionAreaText}" Grid.Row="1" Grid.Column="0" Margin="3" ></TextBlock>
<ComboBox Grid.Column="1" Grid.Row="1" Margin="3"
Name="cmbDistributionArea" BorderThickness="1"
ItemsSource="{Binding Path=ZonesList}"
DisplayMemberPath="Name"
SelectedValuePath="Value" SelectedIndex="0"
IsSynchronizedWithCurrentItem="True" />

和 C#( View 模型)

    #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);
}

请帮忙。提前致谢。

最佳答案

您需要将组合框数据绑定(bind)到 Collection View 而不是基础集合。下面是一个工作示例:

XAML:

<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>>();
}
}

关于c# - WPF ObservableCollection CollectionView.CurrentChanged 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2243967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com