开发者

Bind Textblock text to 2 different properties

开发者 https://www.devze.com 2023-02-19 10:12 出处:网络
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).

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

精彩评论

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