Thursday, November 22, 2012

Align DataGrid Column Contents

Here is the XAML to center align a DataGrid column:
 
<Style TargetType="DataGridCell">    
<Style.Setters>
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style.Setters>
</Style>

Thursday, May 17, 2012

Have Control Fill Parent's Available Space

Use the "HorizontalAlignment" and "VerticalAlignment" properties and set them to "Stretch":

HorizontalAlignment.="Stretch" VerticalAlignment="Stretch".

For example, you could use these properties on a TextBox that is inside a Grid cell

Wednesday, April 25, 2012

WPF GridView Cell Borders

If you populate a GridView programmatically it is not obvious how can set its style.
The best approach I found is to set its style in XAML first, and then assign it in code. For example, this is how you would put borders under each row:

<ListView.Resources>
<Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}"> 
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="#BABABE" />
</Style>
</ListView.Resources>

Then, after you set the DataContext property in code, use:

myListView.ItemContainerStyle = (Style)myListView.Resources["itemstyle"];