c# - 折叠所有内容时隐藏扩展器

标签 c# wpf xaml expander

我有一个 WPF Datagrid,它有一个 Collection View Source,上面有 3 个级别的分组。

我已将数据网格设置为使用 3 个扩展器,如下所示:

Level 1 Expander
<content>
    Level 2 Expander
    <content>
        Level 3 Expander
        <content>

Level 2 和 Level 1 只是组的标题

我有第二个控件,允许用户显示和隐藏 3 级项目,它通过将 3 级扩展器绑定(bind)到后面对象中的 bool “IsVisible”属性来工作。

       <!--  Style for groups under the top level. this is the style for how a sample is displayed  -->
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Margin" Value="0,0,0,0" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">

                                <!--  The parent control that determines whether or not an item needs to be displayed. This holds all of the sub controls displayed for a sample  -->
                                <Expander Margin="2"
                                          Background="{Binding Path=Name,
                                                               Converter={StaticResource SampleTypeToColourConverter}}"
                                          IsExpanded="True"
                                          Visibility="{Binding Path=Items[0].IsVisibleInMainScreen,
                                                               Converter={StaticResource BoolToVisibilityConverter}}">

这种方法非常有效。

然而

如果用户取消选择第 3 级扩展器中的所有项目,第 2 级扩展器标题仍会显示,这意味着宝贵的空间已用完,显示没有可见数据的组标题。

我想要的是一种将 2 级扩展器的可见性绑定(bind)到其子控件的方法,并说“如果所有子项都可见,则显示扩展器,否则折叠它”

这可能吗?

最佳答案

我找到了一种相当简单明了但并不完美的方法来实现您的目标。如果你没有太多的组,这应该可以解决问题。

我刚刚将此触发器添加到 GroupItem ControlTemplate 中:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding ElementName=IP, Path=ActualHeight}" Value="0">
        <Setter Property="Visibility" Value="Hidden"/>
        <Setter Property="Height" Value="1"/>
    </DataTrigger>
</ControlTemplate.Triggers>

ItemsPresenter (IP) ActualSize 降为零时,它将几乎折叠标题。

为什么差不多?

当控件初始化且绑定(bind)发生之前,ItemPresenter ActualHeight 为 0,当 Visibility 设置为 Collapsed 时,ItemPresenter 根本不会呈现。

使用 Visibility.Hidden 允许 ItemsPresenter 进入渲染阶段并进行测量。 我成功地将 Height 降低到 .4 px,但我怀疑这与设备有关。

关于c# - 折叠所有内容时隐藏扩展器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32886594/

相关文章:

c# - WCF RESTful 服务的 Java 和其他客户端

wpf - 如果 View 和 View 模型之间不使用数据绑定(bind),MVVM 可以提供任何优势吗?

c# - 将 XAML ObjectDataProvider 转换为 C#

c# - Listview 中的 TextBox - 在 TextBox TextChanged 上获取 ListViewItem

xaml - Windows Phone 8.1 通用应用程序中的字段验证

c# - ASP.Net MVC 身份忘记密码

c# - Windows 10 : differentiate between preinstalled app and downloaded app

c# - 什么可以更改文件的创建/修改日期?

c# - 将可见性绑定(bind)到与 DataContext 不同的源

c# - WPF Datagrid 获取所有选中的行(不使用 WPF 绑定(bind))