我的 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 中的绑定(bind)是:
<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" />
View 模型中的 TheFilter 是:
public Func<View_Small_Company, string, bool> TheFilter = (o, prefix) => o.Companyname.StartsWith(prefix);
所有编译都没有困难,但依赖属性 -- theFilter -- 仍然为空。
执行此操作的语法是什么?可以吗?
编辑#1。我意识到 xaml 需要绑定(bind)属性,那么一个函数怎么能 作为“属性”传递?
TIA
最佳答案
将 TheFilter 更改为属性:
public Func<View_Small_Company, string, bool> TheFilter { get; set; }
并在构造函数中设置:
TheFilter = (o, prefix) => ((View_Small_Company)o).Companyname.StartsWith(prefix);
从 Func<View_Small_Company, string, bool>
更改 View 模型中的 TheFilter 类型至 Func<object, string, bool>
关于c# - 如何从 View 模型将 Func<T,T,T> 绑定(bind)到 XAML 中的依赖属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32294410/