如何在wpf mvvm中使用组合框 [英] How can I use combobox with wpf mvvm
问题描述
我有一个员工表。员工表中的位置和位置字段。
我必须使用combobox进行过滤。如果我在组合框中选择 A地点,则只有A地点人员应该进入屏幕;如果我选择B地点,则只有B地点人员应该进入屏幕。
这是我的xaml条目和ComboBox。ParticularEntries是我的所有条目(A和B位置在一起)
I have an employee table. and location field in employee table. I have to use combobox to filter that. If I choose "A location" in combobox only A location people should come in screen if I choose B location only B location people should come in screen. It's my xaml Entries and ComboBox.ParticularEntries is my all entries (A and B locations together)
Initialized ParticularEntries就是这样:
Initialized ParticularEntries like that:
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
和EntryReportSpecial Model类:
And EntryReportParticular Model 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
It's my 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相关的部分:
ComboBox:
And It's my related part of ViewModel: ComboBox:
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(根据位置进行过滤,不带按钮)
LocationFilterCommand(to filtered according to location without button)
#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
我是这样做的。我的ComboBox具有A和B位置,但是当我选择A或B位置时,任何更改。如何解决此问题以及如何根据位置进行过滤?我应该在UI或其他应用程序中进行哪些更改?
I did that. I have ComboBox with A and B locations but when I choose A or B location anything changed.How can I fix this and how can I filtered according to location? What should I change in UI or others to do that?
推荐答案
您在 LocationFilter中的代码
毫无意义。
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
它将返回 IEnumerable< bool>
,但是
如果要过滤,则必须使用 Where
。
If you want to filter, you have to use Where
.
但是即使您将代码更改为
But even if you change your code to
ParticularEntries = ParticularEntries.Where(pg => pg.Region.Location == _selectedLocation);
您将看到一个变化,但是下次选择其他位置时,您将面临下一个问题。
you will see a change, but you will face the next problem next time when you select a different location.
您需要一个集合,其中所有未过滤的项存储在私有字段中,并将其用于过滤
You need a collection with all unfiltered items stored inside a private field and use that for filtering.
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();
}
这篇关于如何在wpf mvvm中使用组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!