本文介绍了连接字符串,而不是使用堆栈的TextBlocks的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示在WPF ItemsControl的客户对象的列表。我创建了一个DataTemplate此:

I want to show a list of Customer objects in a WPF ItemsControl. I've created a DataTemplate for this:

    <DataTemplate DataType="{x:Type myNameSpace:Customer}">
        <StackPanel Orientation="Horizontal" Margin="10">
            <CheckBox"></CheckBox>
            <TextBlock Text="{Binding Path=Number}"></TextBlock>
            <TextBlock Text=" - "></TextBlock>
            <TextBlock Text="{Binding Path=Name}"></TextBlock>
        </StackPanel>
    </DataTemplate>

所以,我想基本上是一个简单的列表(带复选框),其中包含数 - 名称。是不是有什么办法,我可以在绑定部分,直接Concat的编号和名称?

So what I want basically is a simple list (with checkboxes) that contains NUMBER - NAME. Isn't there a way in which I can concat the number and name directly in the Binding part?

推荐答案

目前的StringFormat属性(在.NET 3.5 SP1),您或许可以使用。而有用的WPF结合作弊sheat可以找到here.如果没有帮助,您可以永诺编写自己的ValueConverter或自定义属性的对象。

There is StringFormat property (in .NET 3.5 SP1), which you probably can use. And usefull WPF binding cheat sheat can found here. If it doesn't help, you can allways write your own ValueConverter or custom property for your object.

刚才检查时,可以使用的StringFormat与multibinding。在你的情况code将是这样的:

Just checked, you can use StringFormat with multibinding. In your case code will be something like this:

<TextBlock>
  <TextBlock.Text>
    <MultiBinding StringFormat=" {0} - {1}">
        <Binding Path="Number"/>
        <Binding Path="Name"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

我不得不开始格式字符串空间,否则的Visual Studio不会建造,我想你会发现的方式解决它:)

I had to start format string with space, otherwise Visual Studio wouldn't build, but I think you will find way get around it :)

修改
空间是必要的的StringFormat以防止分析器治疗 {0} 作为一个实际的结合。其他选择:

Edit
The space is needed in the StringFormat to keep the parser from treating {0} as an actual binding. Other alternatives:

<!-- use a space before the first format -->
<MultiBinding StringFormat=" {0} - {1}">

<!-- escape the formats -->
<MultiBinding StringFormat="\{0\} - \{1\}">

<!-- use {} before the first format -->
<MultiBinding StringFormat="{}{0} - {1}">

这篇关于连接字符串,而不是使用堆栈的TextBlocks的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 18:11