AndrewBenjamin
AndrewBenjamin

Reputation: 779

Create and style a ListView all in code behind

Enough banging my head on my keyboard. I have a listview that works perfectly like this:

FCView.FCListView.ItemsSource = myItemsSouce;
CollectionView view = CollectionViewSource.GetDefaultView(FCView.FCListView.ItemsSource) as CollectionView;
PropertyGroupDescription gd = new PropertyGroupDescription("Root");
view.GroupDescriptions.Add(gd);

Now all I want to do is make those group headers bold. 3 Hours later, this is the best I can come up with:

Style myStyle = new Style(typeof(GroupItem));    
DataTemplate dt = new DataTemplate();
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(GroupItem));
spFactory.SetValue(GroupItem.FontWeightProperty, FontWeights.Bold);
spFactory.SetValue(GroupItem.ForegroundProperty, new SolidColorBrush(Colors.Red));
dt.VisualTree = spFactory;
GroupStyle groupStyle = new GroupStyle();
groupStyle.HeaderTemplate = dt;
groupStyle.ContainerStyle = myStyle;
FCListView.GroupStyle.Add(groupStyle);

But this overwrites my GroupDescription unless I re-bind it (which seems redundant and isn't working properly either). Is there a simpler way to style the group header (or, just as good, not style the other listview items under the group header)

Upvotes: 0

Views: 885

Answers (1)

Il Vic
Il Vic

Reputation: 5666

Indeed it is not so good using a GroupItem (which is a ContentControl) for datatemplating you header. In your place I would use a simple TextBlock.

The point is that you cannot see the groups descriptions just because your DataTemplate is not binded to them. So just add the commented line:

Style myStyle = new Style(typeof(GroupItem));    
DataTemplate dt = new DataTemplate();
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(GroupItem));
spFactory.SetValue(GroupItem.FontWeightProperty, FontWeights.Bold);
spFactory.SetValue(GroupItem.ForegroundProperty, new SolidColorBrush(Colors.Red));
// You missed next line
spFactory.SetBinding(GroupItem.ContentProperty, new Binding("Name"));
//
dt.VisualTree = spFactory;
GroupStyle groupStyle = new GroupStyle();
groupStyle.HeaderTemplate = dt;
groupStyle.ContainerStyle = myStyle;
FCListView.GroupStyle.Add(groupStyle);

In this way you can bind the Name of the group (i.e. its description) with the GroupItem's content.

The best way to create a template is by using XAML. Anyway if I should use code for some reasons, I will use:

DataTemplate dt = new DataTemplate();
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(TextBlock));
spFactory.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
spFactory.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.Red));
spFactory.SetBinding(TextBlock.TextProperty, new Binding("Name"));
dt.VisualTree = spFactory;
GroupStyle groupStyle = new GroupStyle();
groupStyle.HeaderTemplate = dt;

FCListView.GroupStyle.Add(groupStyle);

I hope it can help you.

Upvotes: 1

Related Questions