Saggio
Saggio

Reputation: 2274

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?

Upvotes: 1

Views: 1856

Answers (3)

BenCr
BenCr

Reputation: 6042

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.

Upvotes: 1

svrist
svrist

Reputation: 7110

Or use <Run/>:

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

Upvotes: 0

CodingGorilla
CodingGorilla

Reputation: 19842

Try this:

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

Upvotes: 7

Related Questions