user763554
user763554

Reputation: 2041

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

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>

Upvotes: 4

Views: 11691

Answers (1)

Hukam
Hukam

Reputation: 1026

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>

Upvotes: 9

Related Questions