1
votes

I am unable to add Summary with group description in another control which is actually linked with the group description binding in wpf.

Summary is available as property in Cluster along with Name, and I want to show it in expander header for all the values

.cs

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;

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

            var clusters = new[]
            {
                new Cluster { valueName = "Front end" , Summary = "Front Summary"},
                new Cluster { valueName = "Middle end" , Summary = "Middle Summary"},
                new Cluster { valueName = "Back end" , Summary = "Back Summary"},
            };

            var collectionView = new ListCollectionView(new[]
            {
                new Server { Cluster = clusters[0], Name = "webshop1" },
                new Server { Cluster = clusters[0], Name = "webshop2" },
                new Server { Cluster = clusters[0], Name = "webshop3" },
                new Server { Cluster = clusters[0], Name = "webshop4" },
                new Server { Cluster = clusters[0], Name = "webshop5" },
                new Server { Cluster = clusters[0], Name = "webshop6" },
                new Server { Cluster = clusters[2], Name = "sql1" },
                new Server { Cluster = clusters[2], Name = "sql2" },
            });

            var groupDescription = new PropertyGroupDescription("Cluster.valueName");

            foreach (var cluster in clusters)
            {
                groupDescription.GroupNames.Add(cluster.valueName);
            }

            collectionView.GroupDescriptions.Add(groupDescription);
            ServersList.ItemsSource = collectionView;

        }

        readonly ObservableCollection<object> Clusters;

        void AddNewCluster_Click(object sender, RoutedEventArgs e)
        {
            Clusters.Add(NewClusterName.Text);
        }
    }

    class Cluster
    {
        public string valueName { get; set; }

        public string Summary { get; set; }
    }

    class Server
    {
        public Cluster Cluster { get; set; }
        public string Name { get; set; }
    }
}

xaml

<Window
    x:Class="EmptyGroups.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Servers by cluster"
    WindowStartupLocation="CenterScreen">

    <DockPanel>

        <WrapPanel DockPanel.Dock="Top" Background="BlanchedAlmond">
            <Label Content="Cluster name:" Margin="10"/>
            <TextBox x:Name="NewClusterName" Text="type new cluster name here" MinWidth="50" BorderThickness="1" Margin="10"/>
            <Button Content="Add cluster" Click="AddNewCluster_Click" Margin="10"/>
        </WrapPanel>

        <ListView x:Name="ServersList">

            <ListView.GroupStyle>
                <GroupStyle HidesIfEmpty="False">
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="GroupItem">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="GroupItem">
                                        <Expander IsExpanded="True">
                                            <Expander.Header>
                                                <TextBlock TextWrapping="Wrap" Margin="0,10,0,5" >
                                                    <Bold><TextBlock Text="{Binding Name}"/></Bold> (<TextBlock Text="{Binding ItemCount}"/> servers) Summary : <TextBlock Text="{Binding Summary}"/>
                                                </TextBlock>
                                            </Expander.Header>
                                            <ItemsPresenter/>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>

            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Server" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Header="Cluster" DisplayMemberBinding="{Binding Cluster.Name}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</Window>

screen shot for current code

expected screen shot

1
If anyone finds this as duplicate please share me the relevant linkRoyal Blue Mayank

1 Answers

0
votes
<TextBlock Text="{Binding Summary}"/>

this TextBlock binding is incorrect. there is no Summary property in header DataContext.

chnage it to

<TextBlock Text="{Binding Items[0].Cluster.Summary}" />