My WPF app has two Tabitems and a SQLite database with a table called studentdata
which has multiple columns.
Inside the 1st tabitem there is a datagrid named dg1
, a combo box and a button. 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
. 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.
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 :
<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();
inside the get
of ComboText
, but what should that get
contain ?
Note: I'm also trying to add combo box autocomplete suggestappend (like winforms) feature to the combo box.
I'm sharing the full code here for better understanding and testing purposes (if anyone wished to).
Can anyone help me with this ?
CodePudding user response:
Replace the ComboText
property in the view model with a property that holds the currently selected 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:
<ComboBox IsEditable="True" Width="300"
ItemsSource="{Binding ComboItems}" DisplayMemberPath="Name"
SelectedItem="{Binding SelectedStudent}">