views:

19

answers:

1

Hi, I have a control that has a "Filter" property that expects a function that defines how the contents of the control should be filtered. so far i am setting the filter in code behind as such:

MyControl.Filter = AddressOf Filters.MyFilter

In this example MyFilter is a shared function in the Filters class with the following signature:

Public Shared Function MyFilter(ByVal obj As Object, ByVal text As String) As Boolean

I would like to set this in xaml. I was thinking of setting the Filters.MyFilter as a static resource and setting it that way:

...Filter="{StaticResource myFilter}"/>

but i cant set Filters.MyFilter as a static resource. Any ideas on how to achieve this?

Thanks,

A: 

You cannot do this directly. XAML doesn't provide a way to refer to functions, other than as event handlers.

You can do it indirectly, by creating an object that has a property of predicate type:

public class FilterOMatic
{
  public Predicate<int> FilterProc
  {
    get { return n => (n % 2) == 0; }
  }
}

(Pardon the C#-ism -- I'm not too familiar with the VB syntax for returning functions. I think it would be something like Return AddressOf Filters.MyFilter but I may be wrong.)

Now you can instantiate the FilterOMatic as a resource and reference its FilterProc property via a binding to that resource:

<Window.Resources>
  <local:FilterOMatic x:Key="fom" />
</Window.Resources>

<MyObject Filter="{Binding FilterProc, Source={StaticResource fom}}" />
itowlson