Skip Navigation LinksHome > View Post

Xaml ObjectDataProvider. They really did think of everything

You may remember a previous post here on theJoyOfCode entitled DataTriggers. Does WPF have an answer for everything?. Well, recently I was working on a WPF application and needed to bind an enumeration (the System.Data.CommandType enum actually, you know: Text, StoredProcedure and TableDirect) to a combobox.

At first you may think you can just bind the actual type to the ItemsSource but, of course, a type is a type - not a collection. Wouldn't it be great if I could execute the Enum type's static GetValues() method which returns an array of the enum entries. Oh yes, that would work a treat.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:data="clr-namespace:System.Data;assembly=System.Data"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    
    <Page.Resources>
        <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="commandTypeOptions">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="data:CommandType" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Page.Resources>

    <ComboBox ItemsSource="{Binding Source={StaticResource commandTypeOptions}}" />
</Page>

Wow.

Of course this is basically the WinForms ObjectDataProvider that you're probably already familiar with. But to see it written out like this just hit me.

Tags: Xaml

 
Josh Post By Josh Twist
10:00 AM
11 May 2007

» Next Post: Accessing dlls in the GAC
« Previous Post: The Difference between GAT and GAX

Comments are closed for this post.

Posted by K venkata Kumar @ 17 Nov 2011 6:09 AM
Hi,

<StackPanel Orientation="Vertical" >
<StackPanel.Resources>
<ObjectDataProvider ObjectType="{x:Type m:StringData}" x:Key="objStrings" MethodName="GetStrings"/>
</StackPanel.Resources>
<ListBox Name="lstStrings" Width="200" Height="300" ItemsSource="{Binding Source={StaticResource objStrings}}" />

</StackPanel>

the above xaml file it say "'m' is an undeclared namespace". Please give the solution

© 2005 - 2014 Josh Twist - All Rights Reserved.