关于WPF:在RowHeader中连接字符串-XAML

concatenate strings in rowheader - xaml

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

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

1
2
3
4
5
6
7
8
9
10
11
12
                   <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

任何帮助将不胜感激。


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

1
2
3
4
 <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属性:

1
2
3
4
5
6
7
8
9
10
11
12
<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仅在目标是string属性

时适用