wpflistviewgridview

How to trigger visibility on stack panels in neighboring columns in a GridView?


There is an expander in column 0.

How can the visibility of the stack panels in the neighboring columns be toggled between Visible and Collapsed based on IsExpanded state of the Expander?

<ListView ItemsSource="{Binding Records}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="Records"
                            Width="120">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate DataType="{x:Type local:Record}">
                            <Expander Header="{Binding Name}">
                                <ItemsControl ItemsSource="{Binding Limits}">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <Border Width="25"/>
                                                <CheckBox/>
                                                <Border Width="5"/>
                                                <TextBlock Text="{Binding Name}"/>
                                            </StackPanel>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </Expander>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
...
                <GridViewColumn Header="HiLimit"
                            Width="80">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate DataType="{x:Type local:Record}">
                            <StackPanel Width="70">
                                <Border Height="21"/>
                                <ItemsControl ItemsSource="{Binding Limits}">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <TextBox Text="{Binding HiLimit}"/>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

GridView columns
enter image description here

I was thinking about using a control template trigger but I'm not sure how to approach that when the cell template needs to be a data template?


Solution

  • Two main ways.

    1. Add a bool property for IsExpanded to the collection element;

    2. Use the DataGridRow property or create an Attached Property for this.

    I show the implementation of the second option using Tag.

        <GridViewColumn.CellTemplate>
            <DataTemplate DataType="{x:Type local:Record}">
                <Expander Header="{Binding Name}"
                          IsExpanded="{Binding Tag, 
                                               Mode=OneWayToSource, 
                                               RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewRowPresenter}}}">
    
        <GridViewColumn.CellTemplate>
            <DataTemplate DataType="{x:Type local:Record}">
                <StackPanel Width="70"
                            Visibility="{Binding Tag, 
                                                 Mode=OneWay, 
                                                 RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewRowPresenter}},
                                                 Converter={StaticResource bollToVisiblyConverter}}">
    

    P.S. I am writing "in a hurry" here in the editor. Please do not scold me too much for possible minor errors.