WPF:在布尔属性上绑定/应用过滤器
我想根据 CheckBox
的 IsSelected
属性将过滤器应用于 ListBox
。
目前我有这样的事情。
XAML
<CheckBox Name="_filterCheckBox" Content="Filter list" Checked="ApplyFilterHandler"/>
<ListBox ItemsSource="{Binding SomeItems}" />
CodeBehind
public ObservableCollection<string> SomeItems { get; private set; }
private void ApplyFilterHandler(object sender, RoutedEventArgs e)
{
if (_filterCheckBox.IsChecked.Value)
CollectionViewSource.GetDefaultView(SomeItems).Filter += MyFilter;
else
CollectionViewSource.GetDefaultView(SomeItems).Filter -= MyFilter;
}
private bool MyFilter(object obj)
{
return ...
}
它可以工作,但这个解决方案感觉就像老式的方式(Windows 窗体)。
问题:
是否可以使用 XAML 中的 Bindings / 来实现此目的?
感谢您抽出时间。
I want to apply a filter to a ListBox
accordingly to the IsSelected
property of a CheckBox
.
At the moment I have something like this.
XAML
<CheckBox Name="_filterCheckBox" Content="Filter list" Checked="ApplyFilterHandler"/>
<ListBox ItemsSource="{Binding SomeItems}" />
CodeBehind
public ObservableCollection<string> SomeItems { get; private set; }
private void ApplyFilterHandler(object sender, RoutedEventArgs e)
{
if (_filterCheckBox.IsChecked.Value)
CollectionViewSource.GetDefaultView(SomeItems).Filter += MyFilter;
else
CollectionViewSource.GetDefaultView(SomeItems).Filter -= MyFilter;
}
private bool MyFilter(object obj)
{
return ...
}
It works but this solution feels like the old-fashioned way (Windows Forms).
Question:
Is it possible to achieve this with Bindings / in XAML?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的唯一方法是在 XAML 中创建一个
ObjectDataProvider
和两个单独的CollectionViewSource
对象。一个视图将应用过滤器,而另一视图则不会。然后,您可以直接绑定到CheckBox.IsChecked
属性并使用自定义IValueConverter
。值转换器将具有 2 个依赖属性 - 均为CollectionViewSource
类型。这些属性可能称为“UnfilteredItems”和“FilteredItems”。在 XAML 中,您可以将未过滤的项目属性设置为未过滤的CollectionViewSource
,并将已过滤的项目属性设置为带有过滤器的项目属性。转换器逻辑本身很简单 - 如果为 true,则返回已过滤的CollectionViewSource
,如果为 false,则返回未过滤的。这个解决方案不是非常优雅,但它可以完成工作。由于
Filter
不是DependencyProperty
并且只能由事件处理程序指定,因此我们的双手在这方面受到了束缚。不过,我不认为你的解决方案是糟糕的。The only way I can think of would be to make an
ObjectDataProvider
and two separateCollectionViewSource
objects in XAML. One view would have the filter applied, the other would not. Then you could bind directly to theCheckBox.IsChecked
property and use a customIValueConverter
. The value converter would have 2 dependency properties- both of typeCollectionViewSource.
These properties might be called, "UnfilteredItems" and "FilteredItems." In XAML, you would set the unfiltered items property to theCollectionViewSource
that was unfiltered, and the filtered items property to the one with the filter. The converter logic itself would be simple- if true, return the filteredCollectionViewSource
, if false, return the unfiltered one.This solution is not extremely elegant, but it would get the job done. Because
Filter
is not aDependencyProperty
and can only be specified by an event-handler, our hands are kind of tied on this one. I don't think your solution is a bad one, though.