WPF和MVVM的新手。我尝试将带有 FilterEventHandler 的过滤器用于 CollectionViewSource 。我试图在属性更改中添加过滤器( p_sSelectedCreatedDate )。问题是当我将过滤器添加到 CollectionViewSource 对象时, FilterEventHandler 委托函数( FilterByCreatedDate )未触发。当我调试时,我可以在代码中看到正在添加过滤器,如下面一行所示。
CVS.Filter += new FilterEventHandler(FilterByCreatedDate);
但是,代码返回时不执行 FilterByCreatedDate 。 请注意,我只发布此问题所必需的代码部分以及继承自 INotifyPropertyChanged 的 BaseViewModel 。
以下是必要的代码。
ScanBatchWindow.xaml
<Window x:Class="BatchManPOC.ScanBatchWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BatchManPOC"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmdBehavior="clr-namespace:BatchManPOC.CmdBehavior"
xmlns:video="clr-namespace:BatchManPOC.Video"
xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d"
Title="ScanBatchWindow"
Height="800"
Width="800">
<Window.Resources>
<CollectionViewSource Source="{Binding p_ListBatches}" x:Key="CVS"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Custom:DataGrid ItemsSource="{Binding Source={StaticResource CVS}}" Margin="8" Grid.Row="0" AutoGenerateColumns="True" IsReadOnly="True">
</Custom:DataGrid>
</Grid>
</Window>
ScanBatchViewModel.cs
public class ScanBatchViewModel : BaseViewModel
{
public ObservableCollection<Batch> p_ListBatches { get; set; }
public CollectionViewSource CVS { get; set; }
private string _p_sSelectedCreatedDate;
public string p_sSelectedCreatedDate {
get
{
return _p_sSelectedCreatedDate;
}
set
{
_p_sSelectedCreatedDate = value;
ApplyFilter(!string.IsNullOrEmpty(_p_sSelectedCreatedDate) ? FilterField.CREATED_DATE : FilterField.NONE);
}
}
public ScanBatchViewModel()
{
p_ListBatches = new ObservableCollection<Batch>();
LoadBatches();
CVS = new CollectionViewSource();
}
private void ApplyFilter(FilterField field)
{
switch (field)
{
case FilterField.CREATED_DATE:
AddCreatedDateFilter();
break;
default:
break;
}
}
public void AddCreatedDateFilter()
{
// see Notes on Adding Filters:
if (p_bCanRemoveCreatedDateFilter)
{
CVS.Filter -= new FilterEventHandler(FilterByCreatedDate);
CVS.Filter += new FilterEventHandler(FilterByCreatedDate);
}
else
{
CVS.Filter += new FilterEventHandler(FilterByCreatedDate);
p_bCanRemoveCreatedDateFilter = true;
}
}
private void FilterByCreatedDate(object sender, FilterEventArgs e)
{
// see Notes on Filter Methods:
var src = e.Item as Batch;
if (src == null)
e.Accepted = false;
else if (string.Compare(p_sSelectedCreatedDate, src.Date) != 0)
e.Accepted = false;
}
}
答案 0 :(得分:0)
CollectionViewSource.Filter
是一个事件。当CollectionViewSource
需要过滤内容时,它将被触发。当CollectionViewSource
更新视图时会发生这种情况。
要更新视图,请调用相应的方法:
CVS.View.Refresh();
请注意,您的方法AddCreatedDateFilter()
可以为Filter
事件添加多个事件处理程序。将为集合中的每个项调用每个事件处理程序。这可能会导致性能问题。并且所有事件处理程序都是相同的,所以这也没用。
因此,请确保您只为此事件添加一个事件处理程序。