将Textblock文本绑定到两个不同的属性

时间:2022-08-07 23:56:24

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).

我有一个绑定的treeview,它显示treeviewitem的一个属性(即displayname)(它是对象的自定义viewmodel)。

Here is the associated xaml:

以下是相关的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?

我怎样才能做到这一点呢?使用multi-binding ?

3 个解决方案

#1


7  

Try this:

试试这个:

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

#2


1  

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.

或者可以向视图模型中添加一个属性,该属性在内部计算全名并绑定到它。

#3


0  

Or use <Run/>:

或者使用< / >运行:

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

#1


7  

Try this:

试试这个:

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

#2


1  

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.

或者可以向视图模型中添加一个属性,该属性在内部计算全名并绑定到它。

#3


0  

Or use <Run/>:

或者使用< / >运行:

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