在rowheader - xaml中连接字符串

时间:2014-02-05 22:07:07

标签: wpf xaml concatenation datagridrowheader

我的问题如下:我有一个绑定到我的数据表的数据网格。我希望Button放在行标题中,我希望Button.Content显示两个字符串的连接。我尝试将这两种解决方案结合起来:thisthis

我个人成功,但结合起来会产生空白Button.Content。这是我试过的

                   <DataGrid.RowHeaderTemplate>
                        <DataTemplate>
                            <Button Width="90">
                                <Button.Content>
                                    <MultiBinding StringFormat=" {0} - {1}">
                                        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}" Path="Item.Index"/>
                                        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}" Path="Item.Category"/>
                                    </MultiBinding>
                                </Button.Content>
                            </Button>
                        </DataTemplate>
                    </DataGrid.RowHeaderTemplate

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试添加TextBlock并绑定其Text属性:

<Button Width="90">
    <Button.Content>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat=" {0} - {1}">
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}" Path="Item.Index"/>
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}" Path="Item.Category"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Button.Content>
</Button>

StringFormat仅适用于target为string属性

的情况

答案 1 :(得分:0)

我认为您的StringFormat语法错误:尝试:

 <MultiBinding StringFormat="{}{0} - {1}">
      <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}" Path="Item.Index"/>
      <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}" Path="Item.Category"/>
 </MultiBinding>