none
How to change the Header of a WPF DataGrid Expander according to values in the Group the Expander is related to? RRS feed

  • Вопрос

  • Hello,
    I am using a  <code>CollectionViewSource</code> as ItemSource for a WPF DataGrid to fill and group the DataGrid: (I fill it with data from a database)
    dataTable = new CollectionViewSource
    {
        Source = Global.dsAuftraege.Tables[0]
    };
                
    dataTable.GroupDescriptions.Add(new PropertyGroupDescription("Auftragsnummer"));

    For the DataGrid Groups I use the following Style:

    <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" Text="{Binding ItemCount}"/>
                                        <TextBlock x:Name="ExpanderHeader3"  Text=""/>
                                    </DockPanel>
                                </Expander.Header>
                                <ItemsPresenter/>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
    </Style>

    As you can see I use an <b>Expander</b> and the <b>Expander.Header</b> consists of 3 TextBlocks. <b>And here comes my problem:</b>
    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 <b>Expander.Header</b> texts <b>bold</b> when there are rows within the Expanders <b>Group</b> with <code>"State"==2</code>.
    What is more: I want to <b>count</b> all the rows within a <b>Group</b> with <code>"State"==2</code> and show the count in <b>"ExpanderHeader3"</b> TextBlock.


    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.



    22 августа 2019 г. 10:46

Ответы

  • 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:

    <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 <b>count</b> or <b>FontWeight</b>.

    Not quite elegant nor efficient, because I loop over every row of the underlying database, but it does what it is supposed to.
    • Помечено в качестве ответа joni7373 23 августа 2019 г. 8:38
    23 августа 2019 г. 8:38

Все ответы

  • Hi   joni7373,

    >>Now I want to make the <b>Expander.Header</b> texts <b>bold</b> when there are rows within the Expanders <b>Group</b> with <code>"State"==2</code>.
    What is more: I want to <b>count</b> all the rows within a <b>Group</b> with <code>"State"==2</code> and show the count in <b>"ExpanderHeader3"</b> TextBlock.


    You could use a converter that's passed the Items property of the group header, Then, check the items and set the FontWeight value and count the "State"==2 row number.


    The following code for your reference.

     <local:TValueConverter x:Key="valueconverter"></local:TValueConverter>
    
         <Expander.Header>
            <TextBlock FontWeight="{Binding Items,Converter={StaticResource valueconverter} }"  Text="{Binding Name}" />
          </Expander.Header>
    
      public class TValueConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                bool resul = false;
                int countrow = 0;
                if (value is ReadOnlyObservableCollection<Object>)
                {
    
                    var items = (ReadOnlyObservableCollection<Object>)value;
                    foreach (group gi in items)
                    {
                        if (gi.CLgroup == "zzz")
                        {
                            resul = true;
                            countrow++;
                        }
                    }
    
                }
               
                if(resul)
                {
                    return FontWeights.Bold;
                }
                else
                {
                    return FontWeights.Normal;
                }
                
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return value;
            }
    

    Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. This will help other members to find the solution quickly if they have faced the similar issue. If you have a new question you can start a new thread  with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.
     


    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    23 августа 2019 г. 6:23
    Модератор
  • 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:

    <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 <b>count</b> or <b>FontWeight</b>.

    Not quite elegant nor efficient, because I loop over every row of the underlying database, but it does what it is supposed to.
    • Помечено в качестве ответа joni7373 23 августа 2019 г. 8:38
    23 августа 2019 г. 8:38