【问题标题】:Filtering an ObservableCollection by using an IValueConverter使用 IValueConverter 过滤 ObservableCollection
【发布时间】:2012-08-04 12:56:56
【问题描述】:

我正在构建一个 Windows 8 Metro 应用程序,我需要在其中过滤 ObservableCollection。遗憾的是,WinRT-Framework 中的 CollectionViewSource-Class 不支持过滤,所以我尝试使用IValueConverter 来实现。

我的 XAML:

<RadioButton Content="this Week" GroupName="AppointmentFilter" IsChecked="True" Name="rbtnFilter"/>
<RadioButton Content="all" GroupName="AppointmentFilter"/>
<ListView ItemsSource="{Binding Appointments, ConverterParameter={Binding rbtnFilter.IsChecked}, Converter={StaticResource Filter}}"/>

我的 IValueConverter:

public class AppointmentListFilter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        ObservableCollection<VMAppointment> appointments = value as ObservableCollection<VMAppointment>;
        bool filter = (bool)parameter;

        if (filter)
        {
            return new ObservableCollection<VMAppointment>(appointments.Where(x => x.Date.CompareTo(DateTime.Now.AddDays(7)) <= 0));
        }
        else
        {
            return appointments;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

在执行IValueConverter 时,参数“parameter”为空,不是布尔值。我做错了什么?

【问题讨论】:

标签: c# xaml data-binding windows-runtime


【解决方案1】:

如果绑定本身不支持绑定,您可以更改 Appointments 属性的类型,使其包含您想要的参数。 如果您的转换器只被调用一次 - 您可以尝试订阅 ObservableProperty 的 CollectionChanged 事件并在每次发生时为 Appointments 引发 PropertyChanged 事件。

【讨论】:

    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多