我有一个员工表。员工表中的位置和位置字段。我必须使用组合框进行过滤。如果我仅在组合框中选择“ A位置”,则应该在屏幕上显示A位置人员。这是我的xaml条目和ComboBox。ParticularEntries是我的所有条目(A和B位置在一起)初始化的ParticleEntry像这样:
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
和EntryReportSpecial模型类:
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特殊条目
这是我的命令组合框。
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
我做到了。我的ComboBox具有A和B位置,但是当我选择A或B位置时,任何更改。如何解决此问题以及如何根据位置进行过滤?我应该在UI或其他方面进行哪些更改?谢谢您的帮助。
您在LocationFilter
中的代码完全没有意义。
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
它返回一个IEnumerable<bool>
,但从未分配。
如果要过滤,则必须使用Where
。
但是即使您将代码更改为
ParticularEntries = ParticularEntries.Where(pg => pg.Region.Location == _selectedLocation);
您将看到一个变化,但是当您选择其他位置时,下一次您将面临下一个问题。
您需要一个集合,其中所有未过滤的项目都存储在一个私有字段中,并将其用于过滤。
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();
}