我创建了两个组合框,一个用于类别,一个用于项目。我想要的是当类别改变时,项目也将被改变。这是我的XAML:
<Window x:Class="ComboBoxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="100" Width="202">
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Name="comboBox1" VerticalAlignment="Top" Width="171"
ItemsSource="{Binding CategoryList}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedCategory}"/>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="0,29,0,0" Name="comboBox2" VerticalAlignment="Top" Width="171"
ItemsSource="{Binding ItemList}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedItem}"/>
</Grid>
</Window>
这是代码隐藏:
namespace ComboBoxTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private List<Categories> _CategoryList = new List<Categories>();
private List<Items> _ItemList = new List<Items>();
private Categories _SelectedCategory;
private Items _SelectedItem;
public MainWindow()
{
InitializeComponent();
DataContext = this;
_CategoryList.Add(new Categories(1, "Car"));
_CategoryList.Add(new Categories(2, "Truck"));
_CategoryList.Add(new Categories(3, "Motorcycle"));
SelectedCategory = _CategoryList[0];
}
public List<Categories> CategoryList
{
get { return _CategoryList; }
}
public List<Items> ItemList
{
get { return _ItemList; }
}
public Categories SelectedCategory
{
get { return _SelectedCategory; }
set
{
_SelectedCategory = value;
OnPropertyChanged("SelectedCategory");
UpdateItems();
OnPropertyChanged("ItemList");
}
}
public Items SelectedItem
{
get { return _SelectedItem; }
set
{
_SelectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
private void UpdateItems()
{
_ItemList.Clear();
switch (_SelectedCategory.ID)
{
case 1:
_ItemList.Add(new Items("BMW"));
break;
case 2:
_ItemList.Add(new Items("Ford"));
break;
case 3:
_ItemList.Add(new Items("Harley"));
break;
default:
Console.WriteLine("Something is wrong");
break;
}
if (_ItemList.Count != 0)
{
SelectedItem = _ItemList[0];
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Categories
{
private int _ID;
private string _Name;
public Categories(int id, string name)
{
_ID = id;
_Name = name;
}
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
public class Items
{
public string _Name;
public Items(string name)
{
_Name = name;
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
}
乍一看,看起来很好。当我从汽车改为卡车时,物品清单从宝马变为福特。问题是如果我花费项目列表然后更改类别,新类别的项目列表将具有上一类别中的项目。我无法解决这个问题。任何帮助表示赞赏。
答案 0 :(得分:0)
我的建议是使用ObservableCollection
而不是简单List<>
。
private ObservableCollection<Categories> _CategoryList = new ObservableCollection<Categories>();
private ObservableCollection<Items> _ItemList = new ObservableCollection<Items>();
private Categories _SelectedCategory;
private Items _SelectedItem;
public MainWindow()
{
InitializeComponent();
DataContext = this;
_CategoryList.Add(new Categories(1, "Car"));
_CategoryList.Add(new Categories(2, "Truck"));
_CategoryList.Add(new Categories(3, "Motorcycle"));
SelectedCategory = _CategoryList[0];
}
public ObservableCollection<Categories> CategoryList
{
get { return _CategoryList; }
}
public ObservableCollection<Items> ItemList
{
get { return _ItemList; }
}
或者,在完成PropertyChanged
更新后,您必须从ItemList
方法提升UpdateItems()
ItemList
。