简体   繁体   English

无法使用 MVVM 中的组合框文本过滤特定的 WPF 数据网格

[英]Cannot filter specific WPF datagrid using combo box text in MVVM

My WPF app has two Tabitems and a SQLite database with a table called studentdata which has multiple columns.我的 WPF 应用程序有两个 Tabitems 和一个 SQLite 数据库,其中包含一个名为studentdata的表,该表具有多个列。

Inside the 1st tabitem there is a datagrid named dg1 , a combo box and a button.在第一个 tabitem 中有一个名为dg1的数据网格、一个组合框和一个按钮。 The 2nd tabitem currently has only a datagrid named dg2 which should display some filtered data, for simplicity sake say, whose PaidFeesOn column is blank only those rows should be displayed in dg2 .第二个 tabitem 目前只有一个名为dg2的数据网格,它应该显示一些过滤后的数据,为了简单起见,它的PaidFeesOn列是空白的,只有那些行应该显示在dg2中。 The combo box should also be filled with unique student names.组合框还应填写唯一的学生姓名。

While on dg1 upon app load should display all the data, but when an item is selected from the combo box and then the button is pressed then dg1 should display only those data whose Name column value matches with the combo box selected item.虽然在应用程序加载时在dg1上应该显示所有数据,但是当从组合框中选择一个项目然后按下按钮时, dg1应该只显示Name列值与组合框所选项目匹配的那些数据。

Now for filtering the datagrid using the combo box text I've done the following in the viewmodel:现在为了使用组合框文本过滤数据网格,我在视图模型中完成了以下操作:

public RelayCommand ComboFilterButtonClicked { get; set; }

public CollectionView ComboFilterView { get; set; }

public List<Students> _myitems;
public List<Students> MyItems
{
    get { return _myitems; }
    set { _myitems = value; }
}

public StudentsViewModel()
{
    //........

    ComboFilterButtonClicked = new RelayCommand(ComboFilterData);
    MyItems = GetStudents();
    ComboItems = CollectionViewSource.GetDefaultView(MyItems);
    ComboFilterView = (CollectionView)CollectionViewSource.GetDefaultView(SData);
                ComboFilterView.Filter = OnComboFilterTriggered;
}

public bool OnComboFilterTriggered(object item)
{
    var lookup = item as Students;

    if (lookup != null)
    {
        string x = "";

        if (!string.IsNullOrEmpty(ComboText))
            return (lookup.Name == x);
    }
    return true;
}

public void ComboFilterData(object param)
{
    CollectionViewSource.GetDefaultView(MyItems).Refresh();
}

and the XAML for the combo box:和组合框的 XAML:

<ComboBox IsEditable="True" Width="300"
        ItemsSource="{Binding ComboItems}" DisplayMemberPath="Name"
        Text="{Binding ComboText,Mode=TwoWay}">
</ComboBox>

But when my app loads the dg1 datagrid is blank and when I select an item from the combo box the combo box text shows texts like System.Collections.Generic.List``1[filterDGs.MainWindow+Students] which is due to(I believe..) return GetStudents().ToString();但是当我的应用程序加载dg1数据网格时,当我 select 组合框中的一个项目时,组合框文本显示像System.Collections.Generic.List``1[filterDGs.MainWindow+Students]这样的文本,这是由于(我相信..) return GetStudents().ToString(); inside the get of ComboText , but what should that get contain?ComboTextget里面,但是get应该包含什么?

Note: I'm also trying to add combo box autocomplete suggestappend (like winforms) feature to the combo box.注意:我也在尝试将组合框自动完成 suggestappend(如 winforms)功能添加到组合框。

I'm sharing the full code here for better understanding and testing purposes (if anyone wished to).我在这里分享完整的代码是为了更好地理解和测试(如果有人愿意的话)。

Can anyone help me with this?谁能帮我这个?

Replace the ComboText property in the view model with a property that holds the currently selected Students object:将视图 model 中的ComboText属性替换为包含当前选定Students object 的属性:

private Students _selectedStudent;
public Students SelectedStudent
{
    get
    {
        return _selectedStudent;
    }
    set
    {
        _selectedStudent = value;
        OnPropertyChanged(nameof(SelectedStudent));
    }
}

public bool OnComboFilterTriggered(object item)
{
    var lookup = item as Students;

    return lookup != null
        && !string.IsNullOrEmpty(_selectedStudent?.Name)
        && lookup.Name == _selectedStudent.Name;
}

public void ComboFilterData(object param)
{
    CollectionViewSource.GetDefaultView(MyItems).Filter = OnComboFilterTriggered;
}

Bind the SelectedItem property of the ComboBox to this property:ComboBoxSelectedItem属性绑定到此属性:

<ComboBox IsEditable="True" Width="300"
    ItemsSource="{Binding ComboItems}" DisplayMemberPath="Name"
    SelectedItem="{Binding SelectedStudent}">

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

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