Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am using a CollectionViewSource as ItemSource for a WPF DataGrid to fill and group the DataGrid: (I fill it with data from a database)

C#
dataTable = new CollectionViewSource
{
    Source = Global.dsAuftraege.Tables[0]
};
            
dataTable.GroupDescriptions.Add(new PropertyGroupDescription("Auftragsnummer"));


For the DataGrid Groups I use the following Style:

XML
<Style x:Key="GroupStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander x:Name="expander" IsExpanded="True"
                                  Background="LightBlue"
                                  Foreground="Black">
                            <Expander.Header>
                                <DockPanel>
                                    <TextBlock Style="{StaticResource HeaderTextBlock}" x:Name="ExpanderHeader1" Text="{Binding Name}" Margin="5,0,0,0" Width="200"/>
                                    <TextBlock x:Name="ExpanderHeader2" FontWeight="Normal" Text="{Binding ItemCount}"/>
                                    <TextBlock x:Name="ExpanderHeader3" FontWeight="Normal" Text=""/>
                                </DockPanel>
                            </Expander.Header>
                            <ItemsPresenter/>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


As you can see I use an Expander and the Expander.Header consists of 3 TextBlocks. And here comes my problem:
In the underlying database I have a column called "State" containing integers from -1 to 2. My DataGrid shows this column too.
Now I want to make the Expander.Header texts bold when there are rows within the Expanders Group with "State"==2.
What is more: I want to count all the rows within a Group with "State"==2 and show the count in "ExpanderHeader3" TextBlock.

What I have tried:

I tried different approaches in XAML but also in Code behind.

XAML:
I tried different Triggers, but couldn't get it running.

Code behind:
I tried to access the Expander in code behind with VisualTreeHelper, but it did not work.


I think it should be possible to solve this in XAML, but since I am new to it I have no idea how to do it.

I am happy about any suggestion.
Thank you.
Posted
Updated 22-Aug-19 22:36pm

Add properties (get;set) to your data context for your text block bindings (Name, ItemCount); query your data for the settings; and set the properties.

If done once (in Loaded, for example) may not need to implement an INotifyPropertyChanged interface.

<TextBlock Style="{StaticResource HeaderTextBlock}" x:Name="ExpanderHeader1" Text="{Binding Name}" Margin="5,0,0,0" Width="200"/>
                                    <TextBlock x:Name="ExpanderHeader2" FontWeight="Normal" Text="{Binding ItemCount}"/>
                                    <TextBlock x:Name="ExpanderHeader3" FontWeight="Normal" 
 
Share this answer
 
Comments
joni7373 22-Aug-19 2:22am    
Thank you for the fast reply. But I don't get it. The text block bindings Name and ItemCount work appropiately.
But I want to change their FontWeight Property and the Text Property of "ExpanderHeader3" according to a column in the DataGrid in the different Groups. My problem is therefore I have to look at the data Group-wise and I have no idea how to do this...
I solved it now using Converters for "Text" and "FontWeight" of the TextBoxes in the Expander.Header binding to the Name of the Group like this:
XML
<TextBlock x:Name="ExpanderHeader3" Width="100"  FontWeight="{Binding Path=Name,Converter={StaticResource fontWeightConverter}}" Text="{Binding Path=Name,Converter={StaticResource nameToActiveConverter}}"/>

The Converters go just through my database search for the "Name" and count the rows where "state"==2. Then they return the respective count or FontWeight.

Not quite elegant nor efficient, because I loop over every row of the underlying database, but it does what it is supposed to.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900