我正在构建一个 Windows 8 Metro 应用程序,我需要在其中过滤一个ObservableCollection
. 遗憾的是,WinRT-Framework 中的 CollectionViewSource-Class 不支持过滤,所以我尝试使用IValueConverter
for 这样做。
我的 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”为空,而不是布尔值。我究竟做错了什么?