Wednesday, July 27, 2011

WPF GridView Cell Borders

If you populate a GridView programmatically, and you want to set its style, it's not obvious.

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

0 comments:

Post a Comment