我正在尝试在 XamDataGrid 中将过滤器设置更改为“包含”而不是“开始于”,是否有任何属性可以实现该功能?
经过大量研究后我无法找到它,如果有人可以帮助我找到我错过的东西,那就太好了。
最佳答案
如果您希望在您的 ViewModel 中进行过滤,这里有一个示例来演示如何使用 ICollectionView
:
public class TestViewModel : INotifyPropertyChanged
{
private string _filterText;
private List<string> _itemsList;
public TestViewModel()
{
_itemsList = new List<string>() { "Test 1", "Test 2", "Test 3" };
this.Items = CollectionViewSource.GetDefaultView(_itemsList);
this.Items.Filter = FilterItems;
}
public ICollectionView Items { get; private set; }
public string FilterText
{
get { return _filterText; }
set
{
_filterText = value;
Items.Refresh();
this.RaisePropertyChanged("FilterText");
}
}
private bool FilterItems(object item)
{
return this.FilterText == null || item.ToString().Contains(this.FilterText);
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
#endregion
}
然后在您的 View 中,您只需 DataBind
TextBox
到 FilterText 属性和 ItemsSource
或 Grid 到 Items 属性(这里用 ListBox 演示):<TextBox x:Name="ItemsFilter" Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Width="100" Margin="10" VerticalAlignment="Center"/>
<ListBox x:Name="ItemsList" ItemsSource="{Binding Items}" Grid.Row="1" Width="200" Margin="10" HorizontalAlignment="Left"/>
关于wpf - 更改 XamDatGrid 的筛选器属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12148432/