如何将Func <t,t,t>绑定到视图模型的XAML中的依赖属性?

时间:2015-08-30 07:51:03

标签: 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中的绑定是:

 <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

1 个答案:

答案 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>