开发者

When binding, is it possible to specify the path on the source object?

In a given binding, is it possible to specify the path on the source object?

It seems like this could avoid a lot of trivial conver开发者_运维知识库ters.

Imagine the following example:

C#:

class foo
{
   bool A
   int B
}

WPF:

<ComboBox
    ItemsSource="ListOfFoos" 
    SelectedItem="{Binding number, SourcePath=B}" />


I guess the SelectedValuePath property is what you are looking for. It's inherited from Selector, so it will work for ComboBoxes, ListBoxes, etc.

Cheers, Alex


2 options:

  1. DisplayMemberPath="B" property of ComboBox
  2. Custom template for each item

like this:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding B}"/>
    </DataTemplate>
</ComboBox.ItemTemplate>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜