concatenate strings in rowheader - xaml

1.1k Views Asked by At

My problem is as follows: I have a datagrid that is bound to my datatable. I want a Button to be placed in the row header, and I want the Button.Content to display the concatenate of two strings. I have tried combining these two solutions: this and this

I have success with each individually, but combined it yields a blank Button.Content. Here's what I've tried

                   <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

Any help would be appreciated.

2

There are 2 best solutions below

0
On BEST ANSWER

Try adding TextBlock and binding its Text property:

<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 only applies if target is a string property

1
On

I think your StringFormat syntax is wrong : try :

 <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>