Click here to Skip to main content
11,926,982 members (54,126 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

How To Bind To Generic Method In XAML

, 17 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
As some of you that have worked with XAML and Generics may know, there is currently no support for Generics in XAML (that is no support for direct binding of methods that use generics).So consider this problemThat I have various bits of static data that are used through out the system, that are he

As some of you that have worked with XAML and Generics may know, there is currently no support for Generics in XAML (that is no support for direct binding of methods that use generics).

So consider this problem

That I have various bits of static data that are used through out the system, that are held as a collection(s) (I am using ObservableCollection<t>) of certain data objects. The WPF UI should be able to bind to these collections, without the need for loads of get {…} properties. Ideally there would be a single method that returned the correct type of static data, as requested by the method. I mean there may be a lot of static data, and sure we could do this by exposing lots of properties over the static data collections, but that somehow seems old fashioned to me. We could of course also just take an Object and return a ObservableCollection, but that to seemed wrong, not enough typing more my liking there. The problem seems to warrant a more generic solution. Wait, doesn’t .NET support generics. Hell yeah, ok cool. So possibly we should be trying for something like the following after all:

1:  public ObservableCollection<T> GetForType<T>()
2:  {
3:      return (ObservableCollection<T>)
4:  }

This seems to fit the problem domain, but how could we do this in XAML.

Unfortunately I do not know a way of dealing with generic method calls in XAML. I know there is a ObjectDataProvider object which has a Method property and allows parameters to be built up in XAML. But this would mean we would need one new ObjectDataProvider with parameters for each type we intended to use the above method for. That’s pretty poor. There must be a better way, surely.

Shown below is an attached property that could be used with a ComboBox control to bind to which will populate the ComboBox.ItemsSource property by calling the generic method.

So all you have to do in the XAML is

 1:  <Window x:Class=”GenericBinding.Window1″
 2:      xmlns=””
 3:      xmlns:x=””
 4:      xmlns:local=”clr-namespace:GenericBinding;assembly=”
 5:      Title=”Window1″ Height=”300″ Width=”300″>
 6:      <Grid>
 9:          <ComboBox
10:         local:ComboBoxProps.BoundCollectionType=”local:Person”/>
13:      </Grid>
14:  </Window>

Where the ComboBoxProps.BoundCollectionTypeProperty attached property is declared liked this

 1:  using System;
 2:  using System.Collections.Generic;
 3:  using System.Linq;
 4:  using System.Text;
 5:  using System.Windows;
 6:  using System.Windows.Controls;
 7:  using System.Reflection;
 8:  using System.Collections;
10:  namespace GenericBinding
11:  {
12:      /// <span class="code-SummaryComment"><summary></span>
13:      /// Provides a mechanism for binding a ComboBox.ItemSource against
14:      /// a generic method within the StaticData singleton. Where the generic
15:      /// method has a signature as follows:
16:      /// 
17:      /// public ObservableCollection<span class="code-SummaryComment"><T> GetForType<T>()</span>
18:      /// <span class="code-SummaryComment"></summary></span>
19:      public class ComboBoxProps
20:      {
21:          #region BoundCollectionType
23:          /// <span class="code-SummaryComment"><summary></span>
24:          /// BoundCollectionType Attached Dependency Property
25:          /// <span class="code-SummaryComment"></summary></span>
26:          public static readonly DependencyProperty
                 BoundCollectionTypeProperty =
27:              DependencyProperty.RegisterAttached(
28:              typeof(Type), typeof(ComboBoxProps),
29:                  new FrameworkPropertyMetadata(null,
30:                      new PropertyChangedCallback(
32:          /// <span class="code-SummaryComment"><summary></span>
33:          /// Gets the BoundCollectionType property.  
34:          /// <span class="code-SummaryComment"></summary></span>
35:          public static Type GetBoundCollectionType
                   (DependencyObject d)
36:          {
37:              return (Type)d.GetValue(
38:          }
40:          /// <span class="code-SummaryComment"><summary></span>
41:          /// Sets the BoundCollectionType property.  
42:          /// <span class="code-SummaryComment"></summary></span>
43:          public static void SetBoundCollectionType(
                   DependencyObject d, Type value)
44:          {
45:              d.SetValue(BoundCollectionTypeProperty, value);
46:          }
48:          /// <span class="code-SummaryComment"><summary></span>
49:          /// Handles changes to the BoundCollectionType property.
50:          /// Uses Reflection to obtain the method within the StaticData singleton class
51:          /// that has the generic method that we need to use to get the values from.
52:          /// The method will be marked with a custom ItemsSourceLookUpMethodAttribute
53:          /// to indicate which method is to be used, to create a Dynamic call to
54:          /// using the correct generic parameter.
55:          /// <span class="code-SummaryComment"></summary></span>
56:          private static void OnBoundCollectionTypeChanged(DependencyObject d,
57:              DependencyPropertyChangedEventArgs e)
58:          {
59:              ComboBox cbSource = d as ComboBox;
60:              Type t = (Type)e.NewValue;
61:              Type[] types = new Type[] { t };
63:              MethodInfo[] methods =
64:                  typeof(StaticData).GetMethods(BindingFlags.Public |
66:              foreach (MethodInfo method in methods)
67:              {
68:                  //Didnt like looking up MethodInfo.Name based on a string as it could
69:                  //change, so use a custom attribute to look for on the method instead
71:                  ItemsSourceLookUpMethodAttribute[] attribs =
72:                      (ItemsSourceLookUpMethodAttribute[])
73:                          method.GetCustomAttributes(
74:                              typeof(ItemsSourceLookUpMethodAttribute), true);
76:                  //is this the correct MethodInfo to invoke
77:                  if (attribs.Length > 0)
78:                  {
79:                      // create the generic method
80:                      MethodInfo genericMethod = method.MakeGenericMethod(types);
81:                      cbSource.ItemsSource =
82:                          (IEnumerable)genericMethod.Invoke(StaticData.Instance,
83:                          BindingFlags.Instance, null, null, null);
84:                  }
85:              }
86:          }
87:          #endregion
88:      }
89:  }

If you want to see a full example you can read more about this on the codeproject article link shown below

Hope this helps you as much as it did me. Enjoy


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.


I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 17 Jun 2009
Article Copyright 2009 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid