【问题标题】:Bind ComboBox to Change Another ComboBox with XAML?使用 XAML 绑定 ComboBox 以更改另一个 ComboBox?
【发布时间】:2017-05-14 04:00:31
【问题描述】:

我制作了一个小示例项目来进行可视化。我有一个项目,其中包含许多相互影响的组合框,我需要将其应用于。

我有两个 ComboBox,NumbersColors

Numbers 中的 SelectedItem 改变了 ItemsSelectedItem颜色中。

如何使用 WPF XAML 绑定 ItemSource 和 SelectedItem?

  1. 使用 ICollection?

  2. 从 ObservableCollection 添加/删除项目?

  3. 创建一个列表作为集合的 ItemSource?

  4. 使用 Add()/Remove() 单独更改项目或交换整个项目 另一个的 ItemSource?

comboBoxNumers = 1、2、3、4

comboBoxColors = 红、绿、蓝

  • 1 → 红色
  • 2 → 绿色
  • 3 → 蓝色
  • 4 → 移除红色、绿色。添加黄色。

  • 1、2 或 3 → 移除黄色(如果存在)。添加红色、绿色(如果不存在)。

1 → 红色

2 → 绿色

4 → 黄色(去除红色/绿色)

我一直在使用的旧 C# 方式:

填充组合框

List<string> NumbersItems = new List<string>() { "1", "2", "3", "4" };
NumbersItems.ForEach(i => comboBoxNumbers.Items.Add(i));

List<string> ColorsItems = new List<string>() { "Red", "Green", "Blue" };
ColorsItems.ForEach(i => comboBoxColors.Items.Add(i));

1 → 红色

// Numbers 1
if ((string)comboBoxNumbers.SelectedItem == "1")
{
    // Remove Yellow if Exists
    if (comboBoxColors.Items.Contains("Yellow")) { 
        comboBoxColors.Items.RemoveAt(comboBoxColors.Items.IndexOf("Yellow")); 
    }

    // Add Red if Does Not Exist
    if (!comboBoxColors.Items.Contains("Red")) { 
        comboBoxColors.Items.Insert(0, "Red"); 
    }

    // Select Red
    comboBoxColors.SelectedItem = "Red";
}

2 → 绿色

// Numbers 2
if ((string)comboBoxNumbers.SelectedItem == "2")
{
    // Remove Yellow if Exists
    if (comboBoxColors.Items.Contains("Yellow")) { 
        comboBoxColors.Items.RemoveAt(comboBoxColors.Items.IndexOf("Yellow")); 
    }

    // Add Green if Does Not Exist
    if (!comboBoxColors.Items.Contains("Green")) { 
        comboBoxColors.Items.Insert(1, "Green"); 
    }

    // Select Green
    comboBoxColors.SelectedItem = "Green";
}

4 → 黄色(去除红色/绿色)

// Numbers 4
if ((string)comboBoxNumbers.SelectedItem == "4")
{
    // Remove Red if Exists
    if (comboBoxColors.Items.Contains("Red")) { 
        comboBoxColors.Items.RemoveAt(comboBoxColors.Items.IndexOf("Red")); 
    }
    // Remove Green if Exists
    if (comboBoxColors.Items.Contains("Green")) {
        comboBoxColors.Items.RemoveAt(comboBoxColors.Items.IndexOf("Green")); 
    }

    // Add Yellow if Does Not Exist
    if (!comboBoxColors.Items.Contains("Yellow")) {
        comboBoxColors.Items.Insert(0, "Yellow"); 
    }

    // Select Yellow
    comboBoxColors.SelectedItem = "Yellow";
}

【问题讨论】:

  • 写一个视图模型。 “从 ObservableCollection 添加/删除项目”。将集合绑定到ComboBox.ItemsSource,永远不要直接接触Items

标签: c# wpf visual-studio xaml combobox


【解决方案1】:

您可以通过在 ViewModel 中设置两个 ICollections 来实现此目的,这些 ICollections 将被设置为窗口的数据上下文。这是更好的方法,专注于数据绑定和 MVVM。 此外,将一个组合框的 SelectedItem 绑定到视图模型中的属性。因此,当从第一个组合中选择一个数字时,它将调用绑定属性的设置器,并且在此设置器内,您可以修改将绑定到第二个组合框的第二个 ICollection (Colors),即

<ComboBox name="numberCmb" ItemsSource = {Binding Numbers} SelectedItem ={Binding SelectedNumber../>

<ComboBox name="colorsCmb" ItemsSource = {Binding Colors} SelectedItem ={Binding SelectedColor../>

在 ViewModel 中

public ICollection Numbers {get;set {RaisePropertyChanged("Numbers")}
public ICollection Colors {get;set {RaisePropertyChanged("Colors")}

public int SelectedNumber 
{
get{ return _selectedNumber; }
set
{
_selectedNumber = value;
RaisePropertyChanged("SelectedNumber");
//
Here Modify the Colors collections by calling other method which can filter or modify Colors using LINQ i.e.
ModifyColorsCollection(value);
    //
}

你可以创建类似的方法

public void ModifyColorsCollection(int number)
{
//Logic to modify Colors collection here only
}

【讨论】:

  • 谢谢,我会试试这个,然后再看看它是怎么回事。
猜你喜欢
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
相关资源
最近更新 更多