3
要綁定與數據集的組合和組合作爲參數取值來填充 另一個組合在WPF我如何組合框與數據集綁定在WPF
要綁定與數據集的組合和組合作爲參數取值來填充 另一個組合在WPF我如何組合框與數據集綁定在WPF
這應該讓你開始。 Window_Loaded
事件用幾行設置一個DataTable,並設置ComboBox
的DataContext
和DisplayMemberPath
。 Countries_SelectionChanged
事件抓取SelectedItem
(如果有),並重置Cities.ItemsSource
屬性作爲方法調用的結果,該方法調用返回IEnumerable<string>
。這個調用可以是任何你想要的(數據庫調用,文件操作等)。希望這可以幫助!
<UniformGrid Rows="2" Columns="2">
<Label Content="Countries" VerticalAlignment="Center"/>
<ComboBox Name="Countries" VerticalAlignment="Center" SelectionChanged="Countries_SelectionChanged" ItemsSource="{Binding}"/>
<Label Content="Cities" VerticalAlignment="Center"/>
<ComboBox Name="Cities" VerticalAlignment="Center"/>
</UniformGrid>
private void Window_Loaded(object sender, RoutedEventArgs e) {
DataTable dt = new DataTable();
dt.Columns.Add("Country", typeof(string));
DataRow firstRow = dt.NewRow();
DataRow secondRow = dt.NewRow();
firstRow["Country"] = "USA";
secondRow["Country"] = "Italy";
dt.Rows.Add(firstRow);
dt.Rows.Add(secondRow);
Countries.DisplayMemberPath = "Country";
Countries.DataContext = dt;
}
private void Countries_SelectionChanged(object sender, SelectionChangedEventArgs e) {
DataRowView dr = Countries.SelectedItem as DataRowView;
if (dr != null) {
Cities.ItemsSource = null;
Cities.ItemsSource = GetCities(dr["Country"].ToString());
}
}
private IEnumerable<string> GetCities(string country) {
if (country == "USA")
return new []{ "New York", "Chicago", "Los Angeles", "Dallas", "Denver" };
if (country == "Italy")
return new[] { "Rome", "Venice", "Florence", "Pompeii", "Naples" };
return new[] { "" };
}
這很好。你有什麼問題? – 2009-08-20 04:23:08
太模糊。如果你想讓人們付出努力的答案,你需要努力解決這個問題。需要更多細節;代碼片段也不會受到傷害。 – Charlie 2009-08-20 04:33:38