简体   繁体   中英

How to bind Func<T,T,T> to dependency property in XAML from view model?

I have a dependency property in my AutoFilteredComboBox as:

 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));

The Binding in the XAML is:

 <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"  />

and TheFilter in the view model is:

 public Func<View_Small_Company, string, bool> TheFilter = (o, prefix) => o.Companyname.StartsWith(prefix);

All compiles without difficutly, but the dependency property-- theFilter -- remains null.

What is the syntax to do this? Can it be done?

Edit#1. I realize the xaml needs properties for binding, so how can a function be passed as a "property" ?

TIA

change TheFilter to property:

public Func<View_Small_Company, string, bool> TheFilter { get; set; }

and set it in constructor:

TheFilter = (o, prefix) => ((View_Small_Company)o).Companyname.StartsWith(prefix);

change type of TheFilter in view model from Func<View_Small_Company, string, bool> to Func<object, string, bool>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM