views:

288

answers:

2

Hi,

I'm having this strange issue with my ItemsControl grouping. I have the following setup:

<ItemsControl Margin="3" ItemsSource="{Binding Communications.View}" >
    <ItemsControl.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander>
                                    <Expander.Header>
                                        <Grid>
                                            <Grid.ColumnDefinitions >
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="Auto" />
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Text="{Binding ItemCount, StringFormat='{}[{0}] '}" FontWeight="Bold" />
                                            <TextBlock Grid.Column="1" Text="{Binding Name, Converter={StaticResource GroupingFormatter}, StringFormat='{}Subject: {0}'}" FontWeight="Bold" />
                                        </Grid>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ItemsControl.GroupStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <TextBlock FontWeight="Bold" Text="{Binding Inspector, Converter={StaticResource NameFormatter}, StringFormat='{}From {0}:'}" Margin="3" />
                <TextBlock Text="{Binding SentDate, StringFormat='{}{0:dd/MM/yy}'}" Grid.Row="1" Margin="3"/>
                <TextBlock Text="{Binding Message }" Grid.Column="1" Grid.RowSpan="2" Margin="3"/>
                <Button Command="vm:CommunicationViewModel.DeleteMessageCommand" CommandParameter="{Binding}"  HorizontalAlignment="Right" Grid.Column="2">Delete</Button>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

In my ViewModel, I expose a CollectionViewSource named 'Communications'. I proceed to adding a grouping patter like so:

Communications.GroupDescriptions.Add(new PropertyGroupDescription("Subject"));

Now, the problem i'm experience is the grouping work fine, but I can't see any items inside the groups. What am I doing wrong? Any pointers would be much appreciated.

A: 

I can't seem to reproduce the problem - I assume you are using a CollectionViewSource? It might be because you bound to the View property directly.

Here's the C# code I used:

public class Communication
{
    public string Subject { get; set; }
    public string Body { get; set; }
}

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        var source = (CollectionViewSource)Resources["Communications"];
        source.Source = new List<Communication>()
        {
            new Communication { Subject = "WPF 4.0", Body = "I love what's happening with 4.0"},
            new Communication { Subject = "WPF 4.0", Body = "I hear the text rendering is the best feature"},
            new Communication { Subject = "Blend 3.0", Body = "Behaviors in Blend 3 change everything"}
        };

        source.GroupDescriptions.Add(new PropertyGroupDescription("Subject"));
    }
}

Here is the XAML - it's the same as yours but with a couple of things removed since I don't have your converters or commands:

<Window 
    x:Class="GroupStyleDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    >
    <Window.Resources>
        <CollectionViewSource x:Key="Communications" />
    </Window.Resources>
    <Grid>
        <ItemsControl Margin="3" ItemsSource="{Binding Source={StaticResource Communications}}" >
            <ItemsControl.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander>
                                            <Expander.Header>
                                                <Grid>
                                                    <Grid.ColumnDefinitions >
                                                        <ColumnDefinition Width="*" />
                                                        <ColumnDefinition Width="Auto" />
                                                    </Grid.ColumnDefinitions>
                                                    <TextBlock Text="{Binding ItemCount, StringFormat='{}[{0}] '}" FontWeight="Bold" />
                                                    <TextBlock Grid.Column="1" Text="{Binding Path=Name}" FontWeight="Bold" />
                                                </Grid>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ItemsControl.GroupStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Body }" Grid.Column="1" Grid.RowSpan="2" Margin="3"/>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </Grid>
</Window>
Paul Stovell
Is there anyway to bind to a ViewModel CollectionViewSource becuase I can't reference the XAML resouces from the VM. And binding to the CollectionViewSource doesn't work without binding directly to the .View
Tri Q
I've just found that out of my style library is defining the ItemsControl style and overrides what i had in my code so its thats why it was not working. I removed the style and things are great! Lesson learned.
Tri Q
A: 

Thanks, that helped me (be careful setting application wide styles they bite!!) Many thanks

Steve
It's not necessary to post a "thank you". Stack Overflow is a Question and Answer site, not a discussion forum. If the question and/or answer were useful to you give them thanks in the form of an up-vote. It's the arrow above the big number to the left of the post.
ChrisF