Juliano Oliveira
Juliano Oliveira

Reputation: 928

Expand Header binding don't show the value properly (WPF/C#)

I'm using data binding to show some data in a listview. I'm using headers to sumarize my data and I want to show a text extracted from my itemsource but doesn't worked. There is some mistake in this portion of code? How can I provide the correct datasource to my wpf?

In Main.xml I fill my list with StructLog.cs class, and on wpf I show the data from each item of the list. The others values work normally, and the header are created, just the text on header expander don't appear.

Main.xml

    List<StructLog> all = new List<StructLog>();
    foreach (ObservableCollection<StructLog> res in Patterns.Results)
    {
        foreach (StructLog r in res)
        {
            all.Add(r);
        }
    }

    lstResults.ItemsSource = all;

    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(all);
    PropertyGroupDescription groupDescription = new PropertyGroupDescription("Pattern");
    view.GroupDescriptions.Add(groupDescription);

StructLog.cs

public class StructLog
{
    public int LineNumber{ get; set;}
    public string LineLog{ get; set;}
    public DateTime Time{ get; set;}
    public string Source{ get; set;}
    public string Type{ get; set;}
    public string Pattern{ get; set;}

    public StructLog(StructLine s,string patternName)
    {
        this.LineNumber = s.LineNumber;
        this.LineLog = s.LineLog;
        this.Time = s.Time;
        this.Source = s.Source;
        this.Type = s.Type;
        this.Pattern = patternName;
    }

}

Window.xaml

<ListView Name="lstResults" Grid.Row="1" IsEnabled="True" Grid.RowSpan="4" DataContext="Results" Grid.ColumnSpan="5" Margin="5,5">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Linha" Width="Auto" DisplayMemberBinding="{Binding LineNumber}" />
            <GridViewColumn Header="Fonte" Width="Auto" DisplayMemberBinding="{Binding Source}" />
            <GridViewColumn Header="Data" Width="Auto" DisplayMemberBinding="{Binding Time}" />
            <GridViewColumn Header="Log" Width="Auto" DisplayMemberBinding="{Binding LineLog}" />
        </GridView>
    </ListView.View>

    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander IsExpanded="True">
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="Teste:" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                            <TextBlock Text="{Binding Pattern}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                         </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

Upvotes: 0

Views: 133

Answers (1)

Ayyappan Subramanian
Ayyappan Subramanian

Reputation: 5366

In order to display a value in the group header, grouped items will in the Items property of the GroupItem class. So you can do binding like Text ="{Binding Items[0].Pattern }". This will bind the value of the first item in the group, since all the values in the group will be similar as the values are grouped by Pattern property.

Try this.

 <ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Expander IsExpanded="True">
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Teste:" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                        <TextBlock Text="{Binding Items[0].Pattern}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                     </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListView.GroupStyle>

Upvotes: 1

Related Questions