C#WPF ICollectionView筛选器未在自定义UserControl上触发

时间:2018-07-26 12:20:38

标签: c# wpf

因此,我开发了带有复选框下拉列表的可编辑组合框。用户可以在组合框的文本框部分中键入内容,它应该过滤项目。但是我遇到了一个问题,即当FilterText更改时,过滤器无法触发。

这是我的Usercontrol XAML。所有代码都得到了简化。

<ComboBox x:Name="cmbBox"  IsEditable="True" IsReadOnly="False" 
ItemsSource="{Binding FilteredItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
Text="{Binding FilterText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsDropDownOpen="{Binding DropDownOpen, Mode=TwoWay}" >

后面的代码如下:

public ICollectionView FilteredItems
{
    get { return (ICollectionView)GetValue(FilteredItemsProperty); }
    set { SetValue(FilteredItemsProperty, value); }
}

public static readonly DependencyProperty FilteredItemsProperty =
        DependencyProperty.Register("FilteredItems", typeof(ICollectionView), typeof(ComboCheckBox), new PropertyMetadata(null));

public string FilterText
{
    get { return (string)GetValue(FilterTextProperty); }
    set { SetValue(FilterTextProperty, value); }
}

public static readonly DependencyProperty FilterTextProperty =
        DependencyProperty.Register("FilterText", typeof(string), typeof(ComboCheckBox), new PropertyMetadata(null));

public ObservableCollection<Item> mItems
{
    get { return (ObservableCollection<Item>)GetValue(mItemsProperty); }
    set { SetValue(mItemsProperty, value); }
}

public static readonly DependencyProperty mItemsProperty =
        DependencyProperty.Register("mItems", typeof(ObservableCollection<Item>), typeof(ComboCheckBox), new PropertyMetadata(null));

public ComboCheckBox()
{
    InitializeComponent();

    FilteredItems = CollectionViewSource.GetDefaultView(mItems);
    // apply filtering delegate
    FilteredItems.Filter = i =>
    {
        // This will be invoked for every item in the underlying collection every time Refresh is invoked
        if (string.IsNullOrEmpty(FilterText)) return true;
        Item m = (Item)i;
        return m.Name.ToLower().Contains(FilterText.ToLower());
    };
}

因此在我的构造函数中,我应用了过滤器委托。下面是我的MainWindow XAML:

<Window.DataContext>
    <local:MainWindowsVM />
</Window.DataContext>
<Grid>
    <dd:ComboCheckBox x:Name="combo" Width="400" Height="30" FilteredItems="{Binding DataSource}" mItems="{Binding OC}" />
</Grid>

和ViewModel:

private ObservableCollection<Item> _oc;
    public ObservableCollection<Item> OC
    {
        get { return _oc; }
        set
        {
            _oc = value;
            OnPropertyChanged("OC");
        }
    }

private ICollectionView _datasouorce;
    public ICollectionView DataSource
    {
        get { return _datasouorce; }
        set
        {
            _datasouorce = value;
            OnPropertyChanged("DataSource");
        }
    }

    public MainWindowsVM()
    {
        oc = new ObservableCollection<Item>();
        oc.Add(new Item { Name = "ACT", IsChecked = false });
        oc Add(new Item { Name = "ADVICENNE", IsChecked = false });
        oc.Add(new Item { Name = "NOVARTIS", IsChecked = false });

        DataSource = CollectionViewSource.GetDefaultView(oc);
    }

使用上面的代码,我能够成功查看下拉列表中的项目,这告诉我DataSource在MainWindow上已正确绑定到FilteredItems,但是由于某些原因,在文本框中键入内容时不会发生过滤器。但是,如果我将过滤器委托放置在主窗口VM中,则它可以工作,但是我希望将此逻辑放在后面的Usercontrol代码中,而不是在MainWindow中。

非常感谢任何建议。

0 个答案:

没有答案
相关问题