how to combine multiple header in wpf datagrid

北城以北 提交于 2019-12-05 20:29:20
icebat

If you`re ok with making it read-only, then the easy way is to have one more property to bind to - StakesAndGame:

public string StakesAndGame
{
   get { return string.Format("{0}   {1}", Stakes, Game); }
}

and just add column that uses it:

<DataGridTextColumn  Width="102" Header="Stakes" Binding="{Binding StakesAndGame}"  />

Just don`t forget to call OnPropertyChanged for that property as well when Stakes or Game change. Or you can write a value converter and combine two columns together with it, but it requires more coding than previous one.

If you need them to be writable, then it`s harder and I really don`t recommend such approach. But if you need it anyway, you could play with DataGridTemplateColumn like in this answer.

You can use a DataGridTemplateColumn to do what you want:

<DataGridTemplateColumn Header="Stakes">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Stakes}" />
                <TextBlock Text="{Binding Path=Game}" />
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!