簡體   English   中英

一個組合框取決於WPF中的其他組合框

[英]one combobox dependent on other combobox in WPF

我將這兩個組合框綁定到2個differnet數據庫表。 第一個綁定到帶有動物類別(例如爬行動物,哺乳動物,兩棲動物等)的一列的表,另一個具有兩個列動物名稱和其各自的動物類別(例如蜥蜴-爬行動物,蛇-爬行動物,狗-哺乳動物,青蛙-兩棲動物) )。 現在我想要的是,當我在combobox1中選擇項目時,combobox2應該具有與在組合框1中選擇的項目與每種動物的各自動物類別相匹配的相應動物列表。

這是我到目前為止所做的

<ComboBox Grid.Column="2" Grid.Row="4" Margin="3,1,3,1"
         Name="comboBox1"
         ItemsSource="{Binding Source={StaticResource NameListData}, Path=Animal}"
         Selectionchanged="comboBox1_Selectionchanged" />
    <ComboBox Grid.Column="2" Grid.Row="5" Margin="3,1,3,1"
         Name="comboBox2"
         ItemsSource="{Binding Source={StaticResource NameListData}, Path=Stream}" />

selection_changed函數

private static string aa;
 private void comboBox1_Selectionchanged(object sender, SelectionchangedEventArgs e)
 {
 aA = comboBox1.SelectedItem.ToString();
 }

 public static string aA
 {
 get { return aa; }
 set { aa = value; }
 }

MainViewModel類中綁定了2個組合框的Collections

DataClasses1DataContext dc = new DataClasses1DataContext();
public List<string> Animal
{
get
{
List<string> facult = new List<string>();

foreach (var a in dc.Faculties)
{ 

facult.Add(a.Animal1.ToString());
}

return facult;
}
}


string selection;

public List<string> Stream
{

get
{
selection = Schooling.Pages.NewStudentGeneral.aA;
List<string> stream = new List<string>();

var q = from c in dc.Streams where c.faculty ==selection select c;
foreach (var b in q )
{
stream.Add(b.stream1);
}

return stream;
}
}

在這里選擇字符串沒有獲得在combobox1中選擇的值,因為每當我硬編碼並指定選擇部分時,如下面的語句所示

1 var q = from c in dc.Streams where c.faculty =="Reptiles" select c; 

將為爬行動物提供各自的動物。

我猜想選擇變量是否正在獲取combobox1的選定值,我的問題已解決。 還是在這里做錯了一切? 請幫忙。

您需要以某種方式告訴第二個ComboBox它需要更新其項目,您可以通過INotifyPropertyChanged / INotifyCollectionChanged實現來執行此操作,也可以手動執行。 由於在第一個ComboBox中有一個用於選擇更改的顯式事件,您也可以手動更新第二個,因此可以通過如下更改處理程序來完成:

 private void comboBox1_Selectionchanged(object sender, SelectionchangedEventArgs e)
 {
      aA = comboBox1.SelectedItem.ToString();
      comboBox2.GetBindingExpression(ComboBox.ItemsSourceProperty).UpdateTarget();
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM