gpt4 book ai didi

更改 View 模型时,WPF Combobox 失去与 SelectedItem 的绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 04:37:04 28 4
gpt4 key购买 nike

我有一个更改对象类型的组合框。选择新类型后,我会销毁旧 View 模型并绑定(bind)到新 View 模型。当我这样做时,组合框不再更新所选项目的属性。

组合框声明如下所示。请注意,此组合框位于 ListView 的 ItemTemplate 中。

<ListView ItemSource="{Binding MyViewModels}">
<ListView.ItemTemplate>
<DataTemplate DataType="viewModels:MyTypeViewModel">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Type:" Width="170"/>
<ComboBox SelectedItem="{Binding Type}"
ItemsSource="{Binding Source={StaticResource MyEnumTypes}}"
Width="150"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

失去绑定(bind)的属性如下所示。请注意,我没有调用 OnPropertyChanged,因为该类使用 Fody.PropertyChanged 包并且该类标有 [ImplementsPropertyChanged]。

    public MyType Type { get; set; }
{
get { return _type; }
set
{
if (_type == value) return;

_type = value;

OnMyTypeChanged?.Invoke(this, new MyTypeChangedEventArgs(_type));
}
}

类型改变的回调看起来像这样。

    private void OnMyTypeChanged(object sender, MyTypeChangedEventArgs args)
{
var index = MyViewModels.IndexOf(sender as MyViewModel);

var selected = SelectedMyViewModel == MyViewModels[index];

var tmp = CreateReplacementViewModelByType(args.Type, MyViewModels[index].Model.Address);

if (selected)
SelectedMyViewModel = tmp;

MyViewModels[index] = tmp;

MyViewModels[index].OnMyTypeChanged += OnMyTypeChanged;
}

我第一次更改对象的类型时, View 模型会正确更新。之后更改组合框的所选项目甚至不会调用属性的 Set 函数。组合框以某种方式保持其旧绑定(bind)。

我的 View 的 DataContext 在代码隐藏的程序开始时分配一次。上下文本身是一个单例管理器类,它执行 View 模型的实际交换。初始化看起来像这样。否则不会更新。

    public MainWindow()
{
InitializeComponent();

//Set the global DataContext
DataContext = MyTypeManager.Instance;
}

MyViewModels 属性在管理器类中声明为一个 observablecollection,如下所示:

    public ObservableCollection<MyViewModel> MyViewModels { get; } = new ObservableCollection<MyViewModel>();

我在 visual studio 2015 中创建了一个测试应用程序,存储库的链接位于此处。 https://github.com/df424/WFPComboBoxTest

最佳答案

要绑定(bind)到 Enum 数据源,首先添加一个具有以下内容的 ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:utility="clr-namespace:MyApp;assembly=MyApp">
<ObjectDataProvider x:Key="MyEnumTypesSource" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="MyApp:MyEnumTypes" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</ResourceDictionary>

然后将资源字典添加到您的窗口或应用程序

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/EnumDataSources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

最后按如下方式添加 ComboBox 绑定(bind)

<ComboBox Height="24" Width="100" ItemsSource="{Binding Source={StaticResource MyEnumTypesSource}}"
SelectedValue="{Binding MyViewModel.MyCurrentEnumValue}" />

ViewModel 属性基本上应该如下所示。

public MyApp.MyEnumTypes MyCurrentEnumValue
{
get { return m_MyCurrentEnumValue; }
set
{
m_MyCurrentEnumValue = value;
OnPropertyChanged("MyCurrentEnumValue");
}
}

关于更改 View 模型时,WPF Combobox 失去与 SelectedItem 的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41552002/

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