开发者

Bind DisplayMemberPath to a function in WPF

开发者 https://www.devze.com 2023-01-14 13:08 出处:网络
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 Mid

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?

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" Width="120" 
          HorizontalAlignment="Left" VerticalAlignment="Top" 
          Margin="467,72,0,0" 
          Name="cboDistrictAttorney" 
          SelectedValuePath="Id" 
          SelectedValue="{Binding DistrictAttorneyId}" 
          DisplayMemberPath="{Binding Source={StaticResource nameFormatter}}" />

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


Another case is DataTemplate, but DataTemplate is more heavier for your application.

0

精彩评论

暂无评论...
验证码 换一张
取 消