简体   繁体   English

基于TextBoxes和CheckBoxes MVVM过滤WPF DataGrid

[英]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.我有 4 个用于过滤 WPF DataGrid 的文本框。 In addition to them I have 2 CheckBoxes.除了它们,我还有 2 个复选框。

Currently I am not getting any errors, but目前我没有收到任何错误,但是

  1. I can only check CheckBox and can't uncheck.我只能选中 CheckBox 而不能取消选中。

在此处输入图片说明

  1. If both Checkboxes are checked = "filters applied" and value is entered to YRNRO "applying more filters" = "inputting values" to other TextBoxes does nothing.如果两个复选框都被选中=“应用过滤器”并且将值输入到 YRNRO“应用更多过滤器”=“输入值”到其他文本框什么都不做。 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.我希望在EnableRowFiltering有 CheckBox 过滤器来覆盖在加载数据之前检查 CheckBoxes 时的情况。 So I can check CheckBox and then load data to DataGrid with calling EnableRowFiltering after loading data.因此,我可以检查 CheckBox,然后在加载数据后调用EnableRowFiltering将数据加载到 DataGrid。

MainWindow.xaml:主窗口.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:这是我当前的代码 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:他们应该改用value关键字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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