当存在隐藏基于触发器显示元素复杂的数据模板的ListView我或ListBox控件的大小不缩水? [英] My ListView or ListBox control size doesn't shrink when there is a complex data template which hides display elements based on triggers?

查看:244
本文介绍了当存在隐藏基于触发器显示元素复杂的数据模板的ListView我或ListBox控件的大小不缩水?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为如下定义每个ListViewItem的一个DataTemplate一个ListView元素。运行时,ListView的高度不倒在视图中的项目,这是不可取的行为:

I have a ListView element with a DataTemplate for each ListViewItem defined as follows. When run, the ListView's height is not collapsed onto the items in the view, which is undesirable behavior:

<DataTemplate x:Key="LicenseItemTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"  />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="{Binding company}"></TextBlock>
        <Grid Grid.Row="1" Style="{StaticResource HiddenWhenNotSelectedStyle}">
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Button Grid.Row="0">ClickIt</Button>
        </Grid>
    </Grid>
</DataTemplate>

外网的第二行已应用的样式看起来像这样。风格的目的是揭露选定数据项的详细视图:

The second row of the outer grid has a style applied which looks like this. The purpose of the style is to expose a detail view of the selected data item :

<Style TargetType="{x:Type Grid}" x:Key="HiddenWhenNotSelectedStyle" >
    <Style.Triggers>
        <DataTrigger
            Binding="{Binding Path=IsSelected, 
                        RelativeSource={
                        RelativeSource 
                        Mode=FindAncestor, 
                        AncestorType={x:Type ListViewItem}
                        }
                        }" 
            Value="False">
            <Setter Property="Grid.Visibility" Value="Collapsed" />
        </DataTrigger>
        <DataTrigger
            Binding="{Binding Path=IsSelected, 
                        RelativeSource={
                        RelativeSource 
                        Mode=FindAncestor, 
                        AncestorType={x:Type ListViewItem}
                        }
                        }" 
            Value="True">
            <Setter
                Property="Grid.Visibility"
                Value="Visible"
            />
        </DataTrigger>
    </Style.Triggers>
</Style>

ListView控件呈现这样的:

The ListView renders like this:

所需的外观是这样的,选择没有的元素时:

The desired appearance is this, when none of the elements are selected:

...与当然,ListView的高度调整,以适应当第二栅极是由选择可见的附加内容。我能做些什么,以获得所需的行为吗?

...with, of course, the ListView's height adjusting to accommodate the additional content when the second grid is made visible by selection. What can I do to get the desired behavior?

推荐答案

虽然与WPF人在TechEd讨论这个问题,我发现一个微软员工这个问题。他很不知所措。

While discussing the problem with WPF people at TechEd, I showed a Microsoft employee this question. He was nonplussed.

我们下载了一个工具,它询问WPF布局和标识的容器作为ListView中的虚拟化堆栈面板元素。

We downloaded a tool which interrogates WPF layouts and identified the container as the "Virtualizing Stack Panel" element in the ListView.

在跟进电子邮件,他写道:这是VirtualizingStackPanel的错,我已经开了一个bug它希望它可以固定在未来的版本(使用的StackPanel)的解决方法应该现在是罚款,。只要你不需要的ListView虚拟化的内容。

In a followup email, he wrote: "This is the fault of VirtualizingStackPanel. I’ve opened a bug about it. Hopefully it can be fixed in a future release. The workaround (using StackPanel) should be fine for now, as long as you don’t need the ListView to virtualize its content.

该漏洞涉及VSP的测量算法的步骤,记得曾经发现的最大规模​​和强制所有未来措施调用至少大报告的大小。在你的情况下,VSP的初始计量的触发器都被炒了鱿鱼,所以它计算大小仿佛一切都清晰可见。当触发火灾和折叠按钮,测量算法计算正确的(小)规模,但随后迫使其结果再次成为大。该评论说,一些关于避免滚动时不必要的重新布局,但code运行时,即使没有滚动回事。

The bug involves a step in VSP’s Measure algorithm that remembers the largest size ever discovered and forces all future Measure calls to report a size at least as large. In your case, the VSP is initially measured before any triggers have fired, so it computes the size as if everything were visible. When the triggers fire and collapse the buttons, the measure algorithm computes the correct (small) size, but then forces the result to be large again. The comment says something about avoiding unnecessary re-layouts while scrolling, but the code is running even when there’s no scrolling going on."

的变通涉及重新模板化这一code ListView的:

The work-around involves re-templating the ListView with this code:

<ListView.ItemsPanel>
  <ItemsPanelTemplate>
    <StackPanel/>
  </ItemsPanelTemplate>
</ListView.ItemsPanel>

此引起所希望列表行为工作,但它携带不具有VirtualizingStackPanel的存储器管理能力的缺点。对于我所用,这是适当的;列表项永远不会在同一时间内超过2000左右。

This caused the list behavior to work as desired, but it carries the disadvantage of not having the memory management capabilities of the VirtualizingStackPanel. For my use, this was appropriate; the list items are never going to exceed 2000 or so at one time.

这篇关于当存在隐藏基于触发器显示元素复杂的数据模板的ListView我或ListBox控件的大小不缩水?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆