What if" /> What if" /> What if"/>

Giving DataGridTextColumn a static value

55 Views Asked by At

In WPF DataGrid, we can bind the DataGridTextColumn value this way.

<DataGridTextColumn Header="MyColumn"
    Binding="{Binding MyColumnValue}" />

What if I wanted to give a static value for this column. Something like

<DataGridTextColumn Header="MyColumn"
    Value="This is a static text" />

I guess it's possible to use a converter, or add an additional property to binded model but I hope there is a simpler way to do this.

1

There are 1 best solutions below

0
mm8 On

Replace the DataGridTextColumn with a DataGridTemplateColumn:

<DataGridTemplateColumn Header="MyColumn">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="This is a static text" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Or the set the source of the binding to a string:

<DataGridTextColumn Header="MyColumn"
                    Binding="{Binding Source='This is a static text'}" />