2016-12-29 67 views
1

我已经做了一个小例子项目可视化。我有一个项目,其中包含许多组合框,这些组合框会影响到我需要应用的其他组合框。绑定ComboBox以更改另一个带有XAML的组合框?

我有两个组合框,编号颜色

的SelectedItem改变项目 & 的SelectedItem颜色

如何使用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→红

1 - Red

2→绿色

2 - Green

4→黄色(删除红/绿)

enter image description here

老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"; 
} 
+0

的方法写一个视图模型。 “从ObservableCollection添加/删除项目”。将集合绑定到'ComboBox.ItemsSource',不要直接触摸'Items'。 –

回答

1

可以通过具有在将被设定为的datacontext的视图模型2个ICollections实现这一你的窗户。这是更好的方法,并专注于数据绑定和MVVM。 另外,将一个组合框的SelectedItem绑定到viewmodel中的一个属性。所以,当一个数将从第一个组合中选择,它会调用绑定属性的setter和此setter里面,你可以修改第二ICollection的(颜色),这将被绑定到第二个下拉框即

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

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

而在视图模型

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 
} 
+0

谢谢,我会试试这个,回过头来看看它。 –

相关问题