TextBlock绑定格式

时间:2013-06-24 12:29:16

标签: c# wpf xaml textblock

如何在我的ComboBox项目中实现此行为:(n): Name

其中n和Name是两个可绑定属性。现在我有n Name

这是我的代码:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <Run Text="{Binding n}" />
            <Run Text="{Binding Name}" />
        </TextBlock>
    </DataTemplate>
</ComboBox.ItemTemplate>

我认为可以添加两个<Run Text="("/>等等,但XAML中必须有更优雅的东西。

2 个答案:

答案 0 :(得分:1)

好的很容易:

 <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}({0}): - {1}">
                                    <Binding Path="n" />
                                    <Binding Path="Name" />
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </ComboBox.ItemTemplate>

答案 1 :(得分:1)

另一种方法是使用多个TextBlock,如下所示:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=...}"></TextBlock>
        <TextBlock Text="{Binding Path=...}"></TextBlock>
    </StackPanel>
</DataTemplate>

您可以使用WrapPanel或任何您喜欢的内容替换StackPanel。

相关问题