[英]WPF multiple Combobox binding with model
我有兩個組合框。 一個是年級類別(例如小學、中學、高中),另一個是年級。 我想確保年份組合框只顯示相關的,這樣就不會有中學四年級這樣的東西。 這就是我實現我的組合框的方式。
模型
public class ComboBoxLogic {
private List<string> _Grade_category;
private List<string> _Year;
// There is no set property because all sets are done within the class.
public IList<string> public IList<string> Grade_category
{
get
{
return _Grade_category;
}
}
public IList<string> Year
{
get
{
return _Year;
}
}
public void Grade_category_selected(string s)
{
string temp;
_Grade.Clear();
if (s.Equals("Elementary"))
{
for (int i = 1; i <= 5; i++)
{
temp = i.ToString();
_Grade.Add(temp);
}
return;
}
else if (s.Equals("Middle"))
{
_Grade.Add("1");
_Grade.Add("2");
_Grade.Add("3");
return;
}
else if (s.Equals("High"))
{
_Grade.Add("1");
_Grade.Add("2");
_Grade.Add("3");
_Grade.Add("4");
return;
}
}
public ComboBoxLogic() {
_Grade_category = new List<string>() {"Elementary", "Middle", "High"}
_Year = new List<string>();
}
}
XAML
<Combobox
Name="Grade_category"
ItemsSource="{Bind Path=Grade_category}"
SelectedItem="{Bind Path=sGrade_category}" />
<Combobox
Name="Year"
ItemsSource="{Bind Path=Year}"
SelectedItem="{Bind Path=sYear}" />
查看模型
class ComboBoxViewModel {
private ComboBoxLogic cbl;
public ComboBoxViewModel() {
cbl = new ComboBoxLogic();
}
public IList<string> Grade_category {
get { return cbl.Grade_category; }
}
public string Grade_category_selected {
set {cbl.Grade_category_selected(value);}
}
public IList<string> Year {
get { return cbl.Year; }
}
public string Year_selected {
set { }
}
}
這涉及2個問題。
當我嘗試這個時,它只在第一次有效。 從年級的第二選擇,年份沒有正確更新。 我想這是因為我沒有在 Grade_category 改變時通知 propertychanged for Year 。 但是,我該怎么做?
在我的項目中,我將擁有多個這樣的組合框,其中一個組合框依賴於另一個組合框。 並且,所有依賴的實現都將寫在 ComboboxLogic 類中。 有沒有辦法讓它更短,而不必為每個組合框編寫 ItemsSource 和 SelectedItem 的每個屬性?
“當我嘗試這個時,它只在第一次起作用。從年級的第二次選擇開始,年份沒有正確更新。我想這是因為我沒有在 Grade_category 更改時通知 propertychanged for Year。但是,我該怎么做去做?”
錯了,這與財產變化無關。 要將集合更改傳播到視圖,該集合必須像ObservableCollection
一樣實現INotifyCollectionChanged
。 用Years:ObservableCollection
替換Years:IList
就可以了。
“在我的項目中,我將有多個這樣的組合框,其中一個組合框依賴於另一個組合框。而且,依賴項的所有實現都將在 ComboboxLogic 類中編寫。有沒有辦法讓它更短,而不必為 ItemsSource 和每個組合框的 SelectedItem?”
在您的特定情況下,訣竅是創建一個反映主題的數據模型。 所有顯示的數據都與單個對象實例相關 - 所選對象(在不同情況下,解決方案是過濾ICollectionView
)。
等級有名稱並持續特定的年數。 Grade
類可以如下所示:
成績.cs
public class Grade : INotifyPropertyChanged
{
public string Name { get; set; }
public IEnumerable<int> Years { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
視圖模型.cs
class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<Grade> Grades { get; set; }
private Grade selectedGrade;
public Grade SelectedGrade
{
get => this.selectedGrade;
set
{
this.selectedGrade = value;
OnPropertyChanged();
}
}
public ViewModel()
{
this.Grades = new ObservableCollection<Grade>
{
new Grade() {Name = "Elementary", Years = Enumerable.Range(1, 4)},
new Grade() {Name = "Middle", Years = Enumerable.Range(1, 3)},
new Grade() {Name = "High", Years = Enumerable.Range(1, 4)},
};
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
主窗口.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DatContext>
<StackPanel>
<ComboBox ItemsSource="{Binding Grades}"
SelectedItem="{Binding SelectedGrade}"
DisplayMemberPath="Name" />
<ComboBox ItemsSource="{Binding SelectedGrade.Years}" />
</StackPanel>
</Window>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.