如何绑定Func从视图模型到XAML中的依赖属性
本文关键字:模型 视图 XAML 属性 依赖 绑定 Func 何绑定 | 更新日期: 2023-09-27 18:10:27
我在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中的Binding是:
<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);
所有编译都没有困难,但是依赖属性——theFilter——仍然为空。
做这个的语法是什么?这能做到吗?
编辑# 1。我意识到xaml需要属性来绑定,那么函数怎么能作为"属性"传递?
TIA
更改TheFilter属性:
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>