rovnyart_
rovnyart_

Reputation: 161

WPF multiple-condition binding filtering

I am new to WPF and all this magical binding and datatriggers stuff, so i ask you for a little help.

I have a simple wpf app shown on picture below. enter image description here

I want my datagrid contents to reflect conditions and date filter. I already figured out how to bind datagrid rows visibility depending on event codes and checkboxes (start, stop, error). But i cant't figure out how to implement date filtering. All i want is: when "Filter by date" checkbox is checked, in my datagrid only those rows remain visible, which have date in "server time" field (i guess i need to parse it somehow from datetime) equal to selected date combobox. Can i achieve that using xaml only? Can enyone help me to do that? Here is xaml for my datagrid:

<DataGrid
        Grid.Row="1"
        Margin="5"
        AutoGenerateColumns="False"
        IsReadOnly="True"
        ItemsSource="{Binding LogEntries}"
        Style="{DynamicResource Helvetica}">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding event_code}" Value="1">
                        <Setter Property="Background" Value="LightGreen" />
                        <Setter Property="Visibility" Value="{Binding IsChecked, ElementName=StartShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding event_code}" Value="2">
                        <Setter Property="Background" Value="LightGray" />
                        <Setter Property="Visibility" Value="{Binding IsChecked, ElementName=StopShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding event_code}" Value="3">
                        <Setter Property="Background" Value="#FFEA816F" />
                        <Setter Property="Visibility" Value="{Binding IsChecked, ElementName=ErrorShowChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </DataTrigger>

                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding server_datetime, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" Header="Server time" />
            <DataGridTextColumn Binding="{Binding user_datetime, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" Header="Client time" />
            <DataGridTextColumn
                Width="*"
                Binding="{Binding log_entry}"
                Header="Entry" />

        </DataGrid.Columns>

Upvotes: 0

Views: 1344

Answers (2)

mm8
mm8

Reputation: 169210

Can i achieve that using xaml only?

No you can't because XAML is a markup language and nothing else.

What you shold do is to bind the SelectedItem of the date ComboBox to a DateTime property of your view model and bind the IsChecked property of the "filter by" CheckBox to a bool property of your view model and filter the LogEntries source collection when the IsChecked source property is set, e.g.:

public class ViewModel : INotifyPropertyChanged
{

    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
            OnPropertyChanged();
            //filter collection:
            LogEntries = allLogEntries.Where(x => x.ServerTime == SelectedDate).ToList();
        }
    }

    private List<LogEntry> _logEntries;
    public List<LogEntry LogEntries
    {
        get { return _logEntries; }
        set
        {
            _logEntries = value;
            OnPropertyChanged();
        }
    }

    //...

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Upvotes: 1

Tesseract
Tesseract

Reputation: 180

I doubt there is a way to do it in XAML only, since filtering requires you to specify how to filter (for example by creating a predicate).
What I recommend is getting an ICollectionView from your ItemsSource (which I assume is an ObservableCollection) and set it's Filter property.
Check out this answer for more details : Filter a DataGrid in WPF

Upvotes: 0

Related Questions