Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagrid column formatting: how to specify multiline and right-align text?

Tags:

wpf

xaml

datagrid

I have a WPF datagrid with 2 columns below. I would like to have the first column displayed in multiple lines when the book title is long and I'd like the price to be right aligned.

Which properties do I set in this code or should I use a template? If I should use a template, I'd like some pointers as I am new to this. Thanks.

   <DataGrid.Columns>
         <DataGridTextColumn Binding="{Binding XPath=BookTitle}" Header="Book Title" />
         <DataGridTextColumn Binding="{Binding XPath=Price}" Header="Price" />
   </DataGrid.Columns>
like image 359
user763554 Avatar asked Sep 06 '25 03:09

user763554


1 Answers

Hope this helps.

<DataGrid.Columns>
    <DataGridTemplateColumn Header="Book Title" Width="150">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock VerticalAlignment="Center" 
                           TextWrapping="Wrap"
                           Text="{Binding BookTitle}"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn >
    <DataGridTemplateColumn Header="Price" Width="100">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock TextAlignment="Right"
                           VerticalAlignment="Center"
                           Text="{Binding Price}"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>
like image 72
Hukam Avatar answered Sep 09 '25 02:09

Hukam