【发布时间】:2014-04-26 19:07:33
【问题描述】:
所以我正在尝试构建一个项目,该项目将允许用户在表单左侧的文本框中键入一些文本,并从我的数据源列表中过滤掉可用的项目。
<Label Content="Enter item name below"></Label>
<TextBox Name="SearchTermTextBox" TabIndex="0" Text="" />
我的印象是我可以将列表绑定到数据源,然后使用转换器过滤掉与字符串不同的项目。
<ListBox DataContext="{Binding Colors}">
<ListBox.ItemsSource>
<MultiBinding Converter="{StaticResource FilterTextValueConverter}" ConverterParameter="{Binding ElementName=SearchTermTextBox, Path=Text}" />
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
//etc...
</ListBox.ItemTemplate>
</ListBox>
但是,除非您使用称为依赖属性的东西,否则您不能绑定到转换器参数中的元素名称。
编辑:看到我对上面的代码造成了混淆,这是我要绑定的转换器:
public class FilterTextValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var trackedColors = value as List<Colors>;
if (trackedColors != null)
return (trackedColors).Where(item => item.ColorName.Contains(parameter.ToString())).ToList();
return null;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Colors
{
public String ColorName;
public String Description;
}
我的方法有什么问题?显然,我激怒了 WPF 众神,因为这是一个相当简单的操作,但原则上我被拒绝了。任何帮助将不胜感激。
【问题讨论】:
标签: c# wpf binding valueconverter