Search code examples
c#wpfexpander

How do I bind the isexpanded property of dynamically created expanders


this is my first entry, so please be patient with me. My problem is that i have Expanders inside a DataGrid. The Expanders are used for Grouping. I also have a filter textfield which filters the View and shows only the matching lines. My problem is: the Grouping expanders isexpanded property should be true when the search finds entries and false if the search is not used. This is my DataGrid.GroupStyle:

<DataGrid.GroupStyle>
  <GroupStyle ContainerStyle="{StaticResource GroupHeaderSettingsStyle}">
         <GroupStyle.Panel>
              <ItemsPanelTemplate>
                   <DataGridRowsPresenter/>
              </ItemsPanelTemplate>
         </GroupStyle.Panel>
  </GroupStyle>
</DataGrid.GroupStyle>

This is the StaticResource

<Style x:Key="GroupHeaderSettingsStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander x:Name="Exp" IsExpanded="{Binding Path=FilterExpander,Mode=TwoWay}">
                            <Expander.Header>
                                <TextBlock Text="{Binding Name}" Foreground="White"/>
                            </Expander.Header>
                            <ItemsPresenter/>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

And this is my C# Property:

public bool? FilterExpander
    {
        get
        {
            return _FilterExpander;
        }
        set
        {
            _FilterExpander = value;
            RaisePropertyChanged(() => FilterExpander);
        }
    }

It never gets into the "get-method", so I think the problem is within the xaml Code. But I am not sure.

I hope you can help me. If I forgot some Code Snippets or information please let me know.

Thanks

What I have tried:

All "modes" All UpdateSourceTriggers, Also RelativeSource Binding


Solution

  • I found the problem. The View didn't find the Property FilterExpander. The Problem was, that Expander looked inside the ViewCollection for the Property. I had to change the binding to this:

    <Style x:Key="GroupHeaderSettingsStyle" TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander x:Name="Exp" IsExpanded="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor},Path=DataContext.FilterExpander}">
                                <Expander.Header>
                                    <TextBlock Text="{Binding Name}" Foreground="White"/>
                                </Expander.Header>
                                <ItemsPresenter/>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    

    Now it works correctly.