如何将组合框与 wpf mvvm 一起使用
How can I use combobox with wpf mvvm
我有一名员工table。和员工 table 中的位置字段。
我必须使用组合框来过滤它。如果我在组合框中选择 "A location" 只有 A 位置的人应该进入屏幕 如果我选择 B 位置只有 B 位置的人应该进入屏幕。
这是我的 xaml 个条目,ComboBox.ParticularEntries 是我的所有条目(A 和 B 位置一起)
像这样初始化特定条目:
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
和EntryReport特定型号Class:
public class EntryReportParticular : BindableItem
{
private Employee _employee;
public Employee Employee
{
get { return _employee; }
set { Set(ref _employee, value); }
}
private DateTime _entry;
public DateTime Entry
{
get { return _entry; }
set { Set(ref _entry, value, () => OnPropertyChanged(nameof(Duration))); }
}
private DateTime _exit;
public DateTime Exit
{
get { return _exit; }
set { Set(ref _exit, value, () => OnPropertyChanged(nameof(Duration))); }
}
public TimeSpan Duration { get { return Exit - Entry; } }
private Region _region;
public Region Region
{
get { return _region; }
set { Set(ref _region, value); }
}
}
这是我的 xaml ParticularEntries
<DataGrid
ItemsSource="{Binding ParticularEntries}"
AutoGenerateColumns="False"
IsReadOnly="True"
RowHeaderWidth="0"
GridLinesVisibility="All"
HorizontalGridLinesBrush="WhiteSmoke"
VerticalGridLinesBrush="WhiteSmoke"
Margin="4">
这是我的命令组合框。
<ComboBox
ItemsSource="{Binding Locations}"
SelectedItem ="{Binding SelectedLocation}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding LocationFilterCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
这是我与 ViewModel 相关的部分:
组合框:
private string _selectedLocation;
public string SelectedLocation
{
get { return _selectedLocation; }
set
{
_selectedLocation = value;
OnPropertyChanged("SelectedLocation");
Trace.WriteLine(SelectedLocation);
}
}
private ObservableCollection<string> _locations;
public ObservableCollection<string> Locations
{
get { return _locations; }
set
{
_locations = value;
OnPropertyChanged("Locations");
}
}
public EntryReportViewModel()//Constructor
{
Locations = new ObservableCollection<string>()
{
"A Location","B Location"
};
}
LocationFilterCommand(根据位置过滤,无需按钮)
#region LocationFilterCommand
private DelegateCommand _locationFilterCommand;
public DelegateCommand LocationFilterCommand
{
get { return _locationFilterCommand ?? (_locationFilterCommand = new DelegateCommand(CanLocationFilter, LocationFilter)); }
}
private bool CanLocationFilter()
{
if (ParticularEntries == null || DailyEntries == null || MonthlyEntries == null)
return false;
return true;
}
private void LocationFilter()
{
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
MonthlyEntries.Select(pg => pg.Employee.CostCenter.Location == _selectedLocation);
}
#endregion
我做到了。我有带有 A 和 B 位置的组合框,但是当我选择 A 或 B 位置时 changed.How 我可以解决这个问题吗?我如何根据位置进行过滤?我应该在 UI 或其他人中更改什么才能做到这一点?
您在 LocationFilter
中的代码完全没有意义。
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
它 returns 一个 IEnumerable<bool>
但它从未被分配。
如果要过滤,就得用Where
。
但即使您将代码更改为
ParticularEntries = ParticularEntries.Where(pg => pg.Region.Location == _selectedLocation);
您会看到变化,但是下次 select 不同的位置时,您将面临下一个问题。
解决方案
您需要一个集合,其中包含存储在私有字段中的所有未过滤项目,并将其用于过滤。
private IEnumerable<EntryReportParticular> _allEntries;
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
private void LocationFilter()
{
ParticularEntries = _allEntries
.Where(pg => pg.Region.Location == _selectedLocation)
.ToList();
}
我有一名员工table。和员工 table 中的位置字段。 我必须使用组合框来过滤它。如果我在组合框中选择 "A location" 只有 A 位置的人应该进入屏幕 如果我选择 B 位置只有 B 位置的人应该进入屏幕。 这是我的 xaml 个条目,ComboBox.ParticularEntries 是我的所有条目(A 和 B 位置一起)
像这样初始化特定条目:
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
和EntryReport特定型号Class:
public class EntryReportParticular : BindableItem
{
private Employee _employee;
public Employee Employee
{
get { return _employee; }
set { Set(ref _employee, value); }
}
private DateTime _entry;
public DateTime Entry
{
get { return _entry; }
set { Set(ref _entry, value, () => OnPropertyChanged(nameof(Duration))); }
}
private DateTime _exit;
public DateTime Exit
{
get { return _exit; }
set { Set(ref _exit, value, () => OnPropertyChanged(nameof(Duration))); }
}
public TimeSpan Duration { get { return Exit - Entry; } }
private Region _region;
public Region Region
{
get { return _region; }
set { Set(ref _region, value); }
}
}
这是我的 xaml ParticularEntries
<DataGrid
ItemsSource="{Binding ParticularEntries}"
AutoGenerateColumns="False"
IsReadOnly="True"
RowHeaderWidth="0"
GridLinesVisibility="All"
HorizontalGridLinesBrush="WhiteSmoke"
VerticalGridLinesBrush="WhiteSmoke"
Margin="4">
这是我的命令组合框。
<ComboBox
ItemsSource="{Binding Locations}"
SelectedItem ="{Binding SelectedLocation}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding LocationFilterCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
这是我与 ViewModel 相关的部分: 组合框:
private string _selectedLocation;
public string SelectedLocation
{
get { return _selectedLocation; }
set
{
_selectedLocation = value;
OnPropertyChanged("SelectedLocation");
Trace.WriteLine(SelectedLocation);
}
}
private ObservableCollection<string> _locations;
public ObservableCollection<string> Locations
{
get { return _locations; }
set
{
_locations = value;
OnPropertyChanged("Locations");
}
}
public EntryReportViewModel()//Constructor
{
Locations = new ObservableCollection<string>()
{
"A Location","B Location"
};
}
LocationFilterCommand(根据位置过滤,无需按钮)
#region LocationFilterCommand
private DelegateCommand _locationFilterCommand;
public DelegateCommand LocationFilterCommand
{
get { return _locationFilterCommand ?? (_locationFilterCommand = new DelegateCommand(CanLocationFilter, LocationFilter)); }
}
private bool CanLocationFilter()
{
if (ParticularEntries == null || DailyEntries == null || MonthlyEntries == null)
return false;
return true;
}
private void LocationFilter()
{
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
MonthlyEntries.Select(pg => pg.Employee.CostCenter.Location == _selectedLocation);
}
#endregion
我做到了。我有带有 A 和 B 位置的组合框,但是当我选择 A 或 B 位置时 changed.How 我可以解决这个问题吗?我如何根据位置进行过滤?我应该在 UI 或其他人中更改什么才能做到这一点?
您在 LocationFilter
中的代码完全没有意义。
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
它 returns 一个 IEnumerable<bool>
但它从未被分配。
如果要过滤,就得用Where
。
但即使您将代码更改为
ParticularEntries = ParticularEntries.Where(pg => pg.Region.Location == _selectedLocation);
您会看到变化,但是下次 select 不同的位置时,您将面临下一个问题。
解决方案
您需要一个集合,其中包含存储在私有字段中的所有未过滤项目,并将其用于过滤。
private IEnumerable<EntryReportParticular> _allEntries;
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
private void LocationFilter()
{
ParticularEntries = _allEntries
.Where(pg => pg.Region.Location == _selectedLocation)
.ToList();
}