c# - 如何从 View 模型将 Func<T,T,T> 绑定(bind)到 XAML 中的依赖属性?

标签 c# wpf xaml

我的 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/

相关文章:

c# - WPF 中带有 INotifyPropertyChanged 的​​绑定(bind)列表

c# - 当按下按钮或按键时,如何在AvalonDock中从一个选项卡转到另一个选项卡?

c# - : if(! 值) 或 if(flag == 值) 哪个更清晰?

c# - MVC 脚手架不加载模型或上下文

c# - ASP.NET MVC 5 页面在数据库调用时挂起

c# - 如何异步加载和显示图像

wpf - 仅在设置颜色后才启动ColorAnimation

c# - 从一页滑动到另一页

c# - TreeView.ItemContainerGenerator.ContainerFromItem 为非根项返回 null。解决方法?

C# 与 Java - 通用列表