Reyn
Reyn

Reputation: 281

WPF ItemControl not binding correctly

I have an ItemControl which bound to the CollectionViewSource. The ItemControl items are grouped then presented in a datatemplate. What I want to achieve is something like this:

-A--------------
| Aa...        |
| Aaaa...      |
----------------

-B--------------
| Bb...        |
| Bbb...       |
----------------

Here is my code:

XAML

<CollectionViewSource x:Key="itembyAlpha" Source="{Binding listItem}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="initial" />
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

    <ItemsControl ItemsSource="{Binding Source={StaticResource itembyAlpha}}">

        <!--GroupStyle-->
        <ItemsControl.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <GroupBox Header="{Binding initial}">
                                        <ItemsPresenter />
                                    </GroupBox>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ItemsControl.GroupStyle>

        <!--Item Template-->
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding title}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>            
    </ItemsControl>

C#

public class Movie
{
   public string id { get; set; }
   public string title { get; set; }
   public string initial { get; set; }
}

List<Movie> lst;
public List<Movie> listItem
{
   get { return lst; }
   set { lst = value; }
}

My problem is, it seems this part of code doesn't work:

<GroupBox Header="{Binding initial}">
   <ItemsPresenter />
</GroupBox>

When I run my program, the result is something like this:

- --------------
| Aa...        |
| Aaaa...      |
----------------

- --------------
| Bb...        |
| Bbb...       |
----------------

The GroupBox's header is blank. It seems the binding doesn't work. Could somebody help me...

Thanks before.

Upvotes: 1

Views: 291

Answers (3)

Mathieu
Mathieu

Reputation: 1677

For binding to work like this, the interface INotifyCollectionChanged should be implemented. I suggest that you use an ObservableCollection instead of a List.

So this code:

List<Movie> lst;
public List<Movie> listItem
{
   get { return lst; }
   set { lst = value; }
}

Will become:

ObservableCollection<Movie> listItem;

Upvotes: 0

dkozl
dkozl

Reputation: 33364

It's because you bind to wrong field. You need to bind to group name and not to the field you group by. Try somethink like this:

<GroupBox Header="{Binding Name}">
    <ItemsPresenter />
</GroupBox>

Each group is a CollectionViewGroup and it has its own properties that you can use when specifying group header.

Upvotes: 1

VidasV
VidasV

Reputation: 4895

Try it like this:

<GroupBox>
   <GroupBox.Header>
        <TextBlock Text="{Binding initial}"/>
   </GroupBox.Header>
        <ItemsPresenter />
   </GroupBox>

Or

    <GroupBox>
   <GroupBox.Header>
        <TextBlock Text="{Binding}"/>
   </GroupBox.Header>
        <ItemsPresenter />
   </GroupBox>

Upvotes: 0

Related Questions