C# 带有一个额外项的级联组合框
我创建了两个组合框,一个用于类别,一个用于项目。我想要的是,当类别更改时,项目也将更改。这是我的XAML:C# 带有一个额外项的级联组合框,c#,wpf,combobox,C#,Wpf,Combobox,我创建了两个组合框,一个用于类别,一个用于项目。我想要的是,当类别更改时,项目也将更改。这是我的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"
<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; }
}
}
}
namespace-comboxtest
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口,INotifyPropertyChanged
{
私有列表_CategoryList=新列表();
私有列表_ItemList=新列表();
私人类别_SelectedCategory;
私人物品_SelectedItem;
公共主窗口()
{
初始化组件();
DataContext=this;
_添加(新类别(1,“汽车”);
_添加(新类别(2,“卡车”);
_添加(新类别(3,“摩托车”);
SelectedCategory=_CategoryList[0];
}
公共列表分类列表
{
获取{return\u CategoryList;}
}
公共列表项目列表
{
获取{return\u ItemList;}
}
公共类别已选择类别
{
获取{return\u SelectedCategory;}
设置
{
_SelectedCategory=值;
OnPropertyChanged(“SelectedCategory”);
UpdateItems();
OnPropertyChanged(“项目列表”);
}
}
公共物品已选定项
{
获取{return\u SelectedItem;}
设置
{
_选择editem=值;
OnPropertyChanged(“SelectedItem”);
}
}
私有void UpdateItems()
{
_ItemList.Clear();
开关(_SelectedCategory.ID)
{
案例1:
_项目列表。添加(新项目(“BMW”);
打破
案例2:
_项目列表。添加(新项目(“福特”);
打破
案例3:
_项目列表。添加(新项目(“哈雷”));
打破
违约:
Console.WriteLine(“有什么不对劲”);
打破
}
如果(_ItemList.Count!=0)
{
选择编辑项=_项列表[0];
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
公共课类别
{
私人内部ID;
私有字符串\u名称;
公共类别(整数id、字符串名称)
{
_ID=ID;
_名称=名称;
}
公共整数ID
{
获取{return\u ID;}
设置{u ID=value;}
}
公共字符串名
{
获取{return\u Name;}
设置{u Name=value;}
}
}
公共类项目
{
公共字符串\u名称;
公共项目(字符串名称)
{
_名称=名称;
}
公共字符串名
{
获取{return\u Name;}
设置{u Name=value;}
}
}
}
乍一看,这似乎很好。当我从汽车换成卡车时,物品清单从宝马换成福特。问题是,如果我扩展项目列表,然后更改类别,则新类别的项目列表将包含前一个类别中的项目。我无法解决这个问题。非常感谢您的帮助。我的建议是使用
可观察收集
而不是简单的列表
私有ObservableCollection _CategoryList=new ObservableCollection();
私有ObservableCollection_ItemList=新ObservableCollection();
私人类别_SelectedCategory;
私人物品_SelectedItem;
公共主窗口()
{
初始化组件();
DataContext=this;
_添加(新类别(1,“汽车”);
_添加(新类别(2,“卡车”);
_添加(新类别(3,“摩托车”);
SelectedCategory=_CategoryList[0];
}
公共可观测集合类别列表
{
获取{return\u CategoryList;}
}
公共可观察收集项目列表
{
获取{return\u ItemList;}
}
或者,在更新完
项目列表后,您必须从UpdateItems()
方法中为您的项目列表提升属性Changed
,谢谢。将列表更改为ObservableCollection可以解决此问题。我还尝试了第二个建议,将OnPropertyChanged(“ItemList”)
移动到UpdateItems()的末尾,但它不起作用。在这种情况下,为什么要支持ObservaleCollection?如果我们将集合绑定到UI,并且我们知道它将在应用程序运行期间发生更改,那么使用ObservaleCollection是有益的,因为它会向UI提供自动collectionchanged通知……否则我们将不得不在应用程序上的每个添加/删除操作上引发更改事件收集
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; }
}