Search code examples
c#wpfxamlmultibindinggridviewcolumn

Using MultiBinding when DataGridColum header was customized


I'm custmozing the DataGridColumns of a ListView.

I have many situations :

1- somtimes, the column header is just one word 'Id', in this case I use this code :

<GridViewColumn Width="Auto"
                DisplayMemberBinding="{Binding Id}"
                Header="Id" />

2- sometimes, the column header is composed of many word, in this case i display them in multilines, using a TextBlock with TextWrapping :

<GridViewColumn Width="85"
                DisplayMemberBinding="{Binding ManyWordsColumnHeader}" >
    <GridViewColumnHeader>
        <TextBlock Text="Many words column header"
                   TextWrapping="Wrap" />
    </GridViewColumnHeader>
</GridViewColumn>

3- sometimes, the content of the lines is Multibinded (many properties in the same time), like this :

<GridViewColumn Header="Fist name & last name">
    <GridViewColumn.CellTemplate>
        <DataTemplate DataType="models:PersonClass">
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0} {1}">
                        <Binding Path="FirstName" />
                        <Binding Path="LastName" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

What should I do if I have a column header name composed of many word and the content of line should be multibinded to many properties ?

In others words : I want to combine the second and the third case together !


Solution

  • I haven't really played with GridView, but this approach seems logical for me to try (just in case you haven't). Simply combine XAML from point 3 with point 2 :

    <GridViewColumn Width="85" >
        <GridViewColumn.CellTemplate>
            <DataTemplate DataType="models:PersonClass">
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} {1}">
                            <Binding Path="FirstName" />
                            <Binding Path="LastName" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </GridViewColumn.CellTemplate>
        <GridViewColumnHeader>
            <TextBlock Text="Many words column header"
                       TextWrapping="Wrap" />
        </GridViewColumnHeader>
    </GridViewColumn>
    

    There you have a column with "column header name composed of many word and the content of line multibinded to many properties".