将文本框文本绑定到2个不同的属性 [英] Bind Textblock text to 2 different properties

查看:160
本文介绍了将文本框文本绑定到2个不同的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定树视图,显示treeviewitem(这是一个对象的自定义视图模型)的属性之一(即displayname)。



这里是相关的xaml:

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

我想要的是在括号内显示名称旁边显示另一个属性。 p>

所以,而不是这样的树形图:

 
-sub node1
--subsub node1
-sub node2

我想要它看起来像这样:

 根(Type1)
-sub node1(Type2)
- 子类节点1(类型3)
-sub node2(类型1)

这个?使用多重绑定?

解决方案

尝试这样:

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


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>

这篇关于将文本框文本绑定到2个不同的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆