views:

61

answers:

1

Hey all,

Is there a way I can bind the DisplayMemberPath of combobox to a function? The object with which I am currently working has three properties I wish to use for presentation; FirstName, LastName, and MiddleName.

I wrote a static method in a Formatting class to handle it. This method is called FullName and accepts three string arguments.

Is there a way I can call Formatting.FullName and pass in the three arguments from my Person object in order to display the formatted full name into the combobox items?

Thanks in advance,
Sonny

UPDATE: I've added the following XAML to the resources section of my page:

<ObjectDataProvider ObjectType="{x:Type business:Formatting}" x:Key="formatter" />

<ObjectDataProvider ObjectInstance="{StaticResource formatter}" MethodName="FullName" x:Key="nameFormatter">
    <ObjectDataProvider.MethodParameters>
        <system:String>Bloggs</system:String>
        <system:String>Joe</system:String>
        <system:String>Q</system:String>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

And my combobox XAML now looks like this:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="467,72,0,0" Name="cboDistrictAttorney" VerticalAlignment="Top" Width="120" SelectedValuePath="Id" SelectedValue="{Binding DistrictAttorneyId}" DisplayMemberPath="{Binding Source={StaticResource nameFormatter}}" />

And the end result is that I have a combobox full of blank items.

+2  A: 

Bea Stollnitz has written an article on this:

http://www.beacosta.com/blog/?m=200603

Scroll down to the "binding to a method" section.

Dave Swersky
How would I use this in conjuction with the DisplayMemberPath property of a combobox? Obviously, I have no Content property with which to work.
Sonny Boy
You can bind any property using that syntax, so you should be able to bind the DisplayMemberPath to the static method on your Formatting class.
Dave Swersky
Updated the topic to show new changes.
Sonny Boy