使其可见后调整 GridView 列的大小
设置:
ListView 和 GridView 在窗口加载时最初是隐藏的,然后在特定的用户操作时变得可见。
目标:
能够设置GridView列的相对宽度。
问题:
我需要的可以通过使用宽度转换器来实现(类似于答案此处),或者通过在ListView(请参阅此解决方案)。 这两种方法似乎都是有效的 - 但仅适用于从早期开始呈现的控件。 就我而言,进行计算时 ActualWidth 始终为 0,并且当 ListView 可见时不会重复这些计算。
所以,我猜,这里真正的问题是当ListView的ActualWidth大于0时如何重新评估列的宽度。
解决方案最好位于XAML 级别,不涉及代码隐藏 - 但这也可以,如果这是唯一的选择;-)
有什么建议吗?
PS 在下面 Chris 的问题之后,这里澄清了 ListView 如何隐藏/显示:它是另一个容器控件的子控件,托管在网格列中,并且该列的宽度由触发器操纵。
<ColumnDefinition>
<ColumnDefinition.Style>
<Style>
<Setter Property="ColumnDefinition.Width" Value="4*"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=OpenItems.Count}" Value="0">
<Setter Property="ColumnDefinition.Width" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ColumnDefinition.Style>
</ColumnDefinition>
我尝试对 ListView 的容器本身应用类似的触发器(以在 Collapsed 和 Visible 之间操纵其可见性),但问题是 Column 不会从其原始 4* 宽度缩小,所以我看到 (empty ) 控制何时应该隐藏它。
The setup:
ListView with GridView that is initially hidden when the window is loaded, and then made visible upon a certain user action.
The aim:
Be able to set relative width of GridView's columns.
The problem:
What I need can be achieved either by using a converter on the width (something similar to the answer here), or by adding a Behavior on the ListView (see this solution).
The both approaches seem to be valid - but only for the controls that are rendered from the early beginning.
In my case, the ActualWidth is always 0 when the calculations are made, and these calculations are not repeated when the ListView is made visible.
So, I guess, the real question here is how to get the columns' Width to be re-evaluated when ListView's ActualWidth gets greater than 0.
The solution would preferably be at the XAML level, without involving the code-behind - but that will do too, if that's the only alternative ;-)
Any suggestions?
P.S. Following Chris's question below, here's a clarification on how the ListView is hidden/shown: it's a child of another container control, hosted in a Grid column, and the width of that column is manipulated by a trigger.
<ColumnDefinition>
<ColumnDefinition.Style>
<Style>
<Setter Property="ColumnDefinition.Width" Value="4*"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=OpenItems.Count}" Value="0">
<Setter Property="ColumnDefinition.Width" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ColumnDefinition.Style>
</ColumnDefinition>
I tried to apply a similar trigger to the ListView's container itself (to manipulate its Visibility between Collapsed and Visible) instead, but the problem with that is that the Column doesn't shrink from its original 4* width, so I see the (empty) control when it's supposed to be hidden.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你如何隐藏/显示ListView?这个问题似乎暗示您是通过将 Width 属性设置为 0 (或非零来显示它)来实现的。相反,您应该尝试使用 可见性 属性(设置为“折叠”以隐藏,或设置为“可见”以显示)。这应该会导致重新评估列的宽度。
更新:
根据您更新的问题,我建议查看此解决方案: http:// /www.codeproject.com/KB/grid/ListView_layout_manager.aspx
我从这里得到它FWIW:WPF:扩展 ListView 的 GridView 的最后一列
How are you hiding/showing the ListView? The question seems to imply that you are doing so by setting the Width property to 0 (or non-zero to show it). Instead, you should try using the Visibility property (set to "Collapsed" to hide, or "Visible" to show). This should cause the column's width to be re-evaluated.
UPDATE:
Based on your updated question, I'd suggest checking out this solution: http://www.codeproject.com/KB/grid/ListView_layout_manager.aspx
I got it from here FWIW: WPF : Extend last column of ListView's GridView