开发者

Bind Textblock text to 2 different properties

I 开发者_运维问答have a binded treeview that displays one of the properties (namely, the displayname) of the treeviewitem (which are custom viewmodel's of an object).

Here is the associated xaml:

<local:ExtendedTreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
                        <TextBlock Text="{Binding OrganDisplayName}" >
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </local:ExtendedTreeView.ItemTemplate>

What I want is to be able to display another property next to the display name in parenthesis.

so instead of the treeview looking like this:

Root
-sub node1
--subsub node1
-sub node2

I want it to look like this:

Root (Type1)
    -sub node1 (Type2)
    --subsub node1 (Type 3)
    -sub node2 (Type 1)

How can I accomplish this? Using multi-binding?


Try this:

<TextBlock>
   <TextBlock.Text>
      <MultiBinding StringFormat="{}{0} ({1})">
          <Binding Path="{YourBindingHere}" />
          <Binding Path="{YourBindingHere}" />
      </MultiBinding>
   </TextBlock.Text>
</TextBlock>


You could just use multiple text blocks

<local:ExtendedTreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding OrganDisplayName}" />
            <TextBlock Grid.Column="1" Text="{Binding TypeName}" />
        </Grid>
    </HierarchicalDataTemplate>
</local:ExtendedTreeView.ItemTemplate>

Or you could add a property to your view model which calculate the full name internally and just bind to that.


Or use <Run/>:

<TextBlock>
  <Run Text="{Binding OrganDisplayName}"/>
  <Run Text=" ("/>
  <Run Text="{Binding TypeName}"/>
  <Run Text=")"/>
</TextBlock>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜