简体   繁体   中英

Filter WPF DataGrid based on TextBoxes and CheckBoxes MVVM

I was hoping to get it working myself but it looks like I am missing something still. I have 4 TextBoxes for filtering WPF DataGrid. In addition to them I have 2 CheckBoxes.

Currently I am not getting any errors, but

  1. I can only check CheckBox and can't uncheck.

在此处输入图片说明

  1. If both Checkboxes are checked = "filters applied" and value is entered to YRNRO "applying more filters" = "inputting values" to other TextBoxes does nothing. Are filters getting messed because of that?

     string ACTIVEBoolquery = ACTIVEBool ? "AND YRNRO Like '6%' OR YRNRO Like '7%'" : ""; this.MainDataTable.DefaultView.RowFilter = $"YRNRO LIKE '{this.YRNROSearchKey}*'" + ACTIVEBoolquery;

I would like to have CheckBox filters inside EnableRowFiltering to cover situation when CheckBoxes are chekced before loading the data. So I can check CheckBox and then load data to DataGrid with calling EnableRowFiltering after loading data.

MainWindow.xaml:

    <!--CHECKBOXES-->

    <CheckBox Style="{StaticResource MyCheckBox}" IsChecked="{Binding ACTIVEBool}" x:Name="ActiveCustomer" Content="" HorizontalAlignment="Left" Margin="128,55,0,0" VerticalAlignment="Top"/>
    <CheckBox Style="{StaticResource MyCheckBox}" IsChecked="{Binding FIANDSEBool}" x:Name="OnlyFIandSE" Content="" HorizontalAlignment="Left" Margin="24,54,0,0" VerticalAlignment="Top"/>

Here is my current code ViewModel.cs:

    // Binding checkbox FIANDSE Bool
    private bool _FIANDSEBool;
    public bool FIANDSEBool
    {
        get => this._FIANDSEBool;
        set
        {
            this._FIANDSEBool = true;
            OnPropertyChanged();

            // Refresh the DataTable filter expression
            EnableRowFiltering();
        }
    }

    // Binding checkbox ACTIVE Bool
    private bool _ACTIVEBool;
    public bool ACTIVEBool
    {
        get => this._ACTIVEBool;
        set
        {
            this._ACTIVEBool = true;
            OnPropertyChanged();

            // Refresh the DataTable filter expression
            EnableRowFiltering();
        }
    }

    public void EnableRowFiltering()
    {
        string FIANDSEBoolquery = FIANDSEBool ? "AND KAYTOSSA LIKE '%1%'" : "";
        string ACTIVEBoolquery = ACTIVEBool ? "AND YRNRO Like '6%' OR YRNRO Like '7%'" : "";

        this.MainDataTable.DefaultView.RowFilter =
          $"YRNRO LIKE '{this.YRNROSearchKey}*'" +
          $"AND HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'" +
          $"AND KONSERNI LIKE '{this.GROUPSearchKey}*'" +
          $"AND LY LIKE '{this.BUSINESSIDSearchKey}*'" +
          FIANDSEBoolquery + ACTIVEBoolquery;
    }

The assignments

this._FIANDSEBool = true;

and

this._ACTIVEBool = true;

in the property setters are obviously wrong.

They should use the value keyword instead:

public bool FIANDSEBool
{
    get => _FIANDSEBool;
    set
    {
        _FIANDSEBool = value; // here
        OnPropertyChanged();
        EnableRowFiltering();
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM