繁体   English   中英

WPF ComboBox 选择导致在第二个 ComboBox 中选择

[英]WPF ComboBox selection causes selection in second ComboBox

我将两个组合框绑定到同一个 listviewcollection。 问题是在一个组合框中选择一个值会导致另一个组合框所选项目更改为第一个组合框的确切值。 它们是耦合的,我希望它们彼此独立。

MyListViewCollection 是这样的

modelsView = new ListCollectionView(MainVM.All_Models);

All_Models是一个像这样的自定义对象的 Observable 集合

public ObservableCollection<MLModel> All_Models { get; set; } = new ObservableCollection<MLModel>() { };

我像这样将两个 ComboBoxes 绑定到modelsview

<ComboBox ItemsSource="{Binding Path=modelsView}" SelectedItem="{Binding SelectedModel_Right}" SelectionChanged="RightSideModelSelection">

<ComboBox ItemsSource="{Binding Path=modelsView}" SelectedItem="{Binding SelectedModel_Left}" SelectionChanged="LeftSideModelSelection">

所以一切正常,组合框包含模型视图中的相同项目列表,这正是我想要的。 我肯定已将所选项目绑定到视图模型中的两个单独的属性,这些是

    private MLModel _selectedModel_left;
    public MLModel SelectedModel_Left
    {
        get { return _selectedModel_left; }
        set
        {
            SetProperty(ref _selectedModel_left, value);
        }
    }

    private MLModel _selectedModel_right;
    public MLModel SelectedModel_Right
    {
        get { return _selectedModel_right; }
        set
        {
            SetProperty(ref _selectedModel_right, value);
        }
    }

我唯一能想到的是它们都引用了集合中的同一个对象,但我不确定它是如何导致这种行为的。 还期望的行为是每个组合框都能够从同一集合中选择和显示不同的项目。

任何解释以及将两个组合框选择相互分离的推荐方法都会有所帮助。

编辑:根据这里的要求是创建最小工作示例的代码

主窗口.xaml

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Orientation="Vertical">
            <ComboBox ItemsSource="{Binding Path=modelsView}" SelectedItem="{Binding SelectedModel_Left}" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}"></TextBlock>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <ComboBox ItemsSource="{Binding Path=modelsView}" SelectedItem="{Binding SelectedModel_Right}" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}"></TextBlock>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </StackPanel>
    </Grid>
</Window>

主窗口.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new ViewModel();
        }
    }
}

MLModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication3
{
    public class MLModel
    {
        public string Name { get; set; }
        public string Type { get; set; }
    }
}

视图模型.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WpfApplication3
{
    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public ViewModel()
        {
            modelsView = new ListCollectionView(All_Models);
            //modelsView.Filter = modelsFilter;
        }
        public ListCollectionView modelsView { get; set; }

        public ObservableCollection<MLModel> All_Models { get; set; } = new ObservableCollection<MLModel>() {
            new MLModel() { Name = "One", Type = "TypeOne" },
            new MLModel() {Name = "Two", Type = "TypeTwo" },
            new MLModel() {Name = "Three", Type = "TypeThree" }
        };

        private MLModel _selectedModel_left;
        public MLModel SelectedModel_Left
        {
            get { return _selectedModel_left; }
            set
            {
                this._selectedModel_left = value;
                NotifyPropertyChanged();
            }
        }

        private MLModel _selectedModel_right;


        public MLModel SelectedModel_Right
        {
            get { return _selectedModel_right; }
            set
            {
                this._selectedModel_right = value;
                NotifyPropertyChanged();
            }
        }
    }
}

文档

如果目标是 ItemsControl,则当前项目与所选项目同步。

您在两个ComboBox对象之间共享相同的ListCollectionView 这意味着当前选定的项目在视图中更新,并在使用相同视图的任何其他ItemsControl中复制。

如果您不希望出现这种行为,则需要为每个ComboBox其自己的ListCollectionView对象以绑定到。

或者,您可以将每个ComboBoxIsSynchronizedWithCurrentItem属性设置为false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM