在数据网格中显示结果取决于组合框选择的值 [英] Show result in data grid depend on combo box selected value
问题描述
i面临一个问题我无法解决我搜索了很多但它不起作用,我使用mvvm我成功加载了我的数据网格中的所有员工细节,我有一个组合框加载性别来过滤带有性别的数据网格行但数据网格没有更新以显示新列表,我使用了Inotifychangedproperty但它没用我显示我的代码建议我
我尝试过:
i am facing a problem i can not solve i searched a lot but it doesn't work, i using mvvm i successfully loaded all the employee details in my datagrid and i have a combobox loading it with gender to filter the datagrid rows with the gender but datagrid does not update to show the new list , i used Inotifychangedproperty but it is useless i showing my code to advice me
What I have tried:
public EditaEmployeesViewModel()
{
SelectionChangedCommand = new RelayCommand(cbGenderSelectedChanged);
_EmployeeList = new ObservableCollection<Model.Employees>
(DataAccess.EmplyeeDatabaseLayer.GetEmployeeFromDataBase());
_GenderList = new ObservableCollection<Model.Gender>
(DataAccess.GenderDataBaseLayer.GetGenderFromDataBase());
}
ObservableCollection<Model.Employees> _EmployeeList;
ObservableCollection<Model.Gender> _GenderList;
public RelayCommand SelectionChangedCommand { get; set; }
void cbGenderSelectedChanged(object parameter)
{
_EmployeeList = new ObservableCollection<model.employees>
(DataAccess.EmplyeeDatabaseLayer.GetEmployeesWithGenderID(SelectedGenderID));
}
public ObservableCollection<model.employees> EmployeeList
{
get
{
return _EmployeeList;
}
set
{
_EmployeeList = value;
Notifypropertychange("EmployeeList");
}
}
public ObservableCollection<model.gender> GenderList
{
get
{
return _GenderList;
}
set
{
_GenderList = value;
Notifypropertychange("GenderList");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void Notifypropertychange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
这里是我的组合框和数据网格
here is my combobox and datagrid
<ComboBox ItemsSource="{Binding GenderList,UpdateSourceTrigger=Explicit}"
DisplayMemberPath="GenderName" SelectedValuePath="ID"
Grid.Column="1" Name="cbGender" SelectedValue="{Binding
SelectedGenderID}" IsSynchronizedWithCurrentItem="True" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding
SelectionChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<DataGrid Grid.Row="2" AutoGenerateColumns="False" CanUserAddRows="False"
SelectedItem="{Binding SelectedEmployee}"
ItemsSource="{Binding EmployeeList,UpdateSourceTrigger=PropertyChanged}"
SelectionMode="Single" SelectedIndex="{Binding SelectedIndex}"
IsSynchronizedWithCurrentItem="True">
推荐答案
你当你在ComboBox中选择一个项目时,会破坏数据绑定规则。
You're breaking Data Binding rules when you select an item in your ComboBox.
void cbGenderSelectedChanged(object parameter)
{
_EmployeeList = new ObservableCollection<model.employees>
(DataAccess.EmplyeeDatabaseLayer.GetEmployeesWithGenderID(SelectedGenderID));
}
如果你有一个通过数据绑定绑定到UI的集合对象,重新创建集合将破坏绑定。
所以要修复,你需要做类似的事情:
If you have a collection object bound to the UI via Data Binding, Recreating the collection will break the binding.
So to fix, you need to do something like:
void cbGenderSelectedChanged(object parameter)
{
_EmployeeList.Clear();
for each (var item in DataAccess.EmplyeeDatabaseLayer
.GetEmployeesWithGenderID(SelectedGenderID))
{
_EmployeeList.add(item);
}
}
这篇关于在数据网格中显示结果取决于组合框选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!