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"];

No comments:

Post a Comment