2016-12-29 34 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

謝謝,我會試試這個,回過頭來看看它。 –

相關問題