最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wpf - How to trigger visibility on stack panels in neighboring columns in a GridView? - Stack Overflow

programmeradmin3浏览0评论

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

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?

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

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?

Share Improve this question edited Mar 17 at 15:50 EldHasp 7,9702 gold badges10 silver badges31 bronze badges asked Mar 17 at 13:42 JimJrJimJr 31 bronze badge 2
  • Using INotifyPropertyChanged in the view model I can bind the IsExpanded state of the Expander to change another property that binds to the Visibility state on each stack panel which works. But it maybe isn't such good practice to use the VM in this way to handle a behaviour that is only related to the view? – JimJr Commented Mar 17 at 16:07
  • Not quite clear. You have two stack panels. One is under the expander, so you don't need to collapse it. Do you want to collapse another panel in the second GridViewColumn when the expander in the first columns is collapsed? – Sergey A Kryukov Commented Mar 18 at 4:24
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论