我的AutoFilteredComboBox中有一个依赖属性:
public Func<object, string, bool> theFilter
{
get { return (Func<object, string, bool>)GetValue(theFilterProperty); }
set { SetValue(theFilterProperty, value); }
}
// Using a DependencyProperty as the backing store for theFilter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty theFilterProperty =
DependencyProperty.Register(
"theFilter",
typeof(Func<object, string, bool>),
typeof(AutoFilteredComboBox),
new UIPropertyMetadata(null));
XAML中的绑定是:
<wc:AutoFilteredComboBox
ItemsSource="{Binding PrimaryInsurance.Companies}"
ItemTemplate="{StaticResource CompanyTemplate}"
IsEditable="True"
IsTextSearchEnabled="True"
TextSearch.TextPath="Companyname"
IsTextSearchCaseSensitive="False"
theFilter="{Binding PrimaryInsurance.TheFilter}"
Height="20" HorizontalAlignment="Left" Margin="162,235,0,0" VerticalAlignment="Top" Width="411" />
和视图模型中的TheFilter是:
public Func<View_Small_Company, string, bool> TheFilter = (o, prefix) => o.Companyname.StartsWith(prefix);
所有编译都没有difficutly,但依赖属性 - theFilter - 仍然为null。
这样做的语法是什么?可以吗?
编辑#1。我意识到xaml需要绑定属性,所以如何才能实现功能 被作为“财产”传递?
TIA
答案 0 :(得分:2)
将TheFilter更改为property:
public Func<View_Small_Company, string, bool> TheFilter { get; set; }
并在构造函数中设置:
TheFilter = (o, prefix) => ((View_Small_Company)o).Companyname.StartsWith(prefix);
将视图模型中的TheFilter类型从Func<View_Small_Company, string, bool>
更改为Func<object, string, bool>