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 like that:
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
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); }
}
}
It's my xaml ParticularEntries
<DataGrid
ItemsSource="{Binding ParticularEntries}"
AutoGenerateColumns="False"
IsReadOnly="True"
RowHeaderWidth="0"
GridLinesVisibility="All"
HorizontalGridLinesBrush="WhiteSmoke"
VerticalGridLinesBrush="WhiteSmoke"
Margin="4">
And It's my combobox with command.
<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>
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(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
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?
Your code in LocationFilter
make no sense at all.
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
It returns an IEnumerable<bool>
but it is never assigned.
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.