ColumnDefinition 在调整大小之前不会扩展

发布于 2024-08-18 16:12:51 字数 1545 浏览 9 评论 0原文

我有一个网格,其中一个列宽定义为 *。 其他列定义为Auto。 具有* 定义的列包含一个从Panel 派生的用户控件,该控件也实现了IScrollInfo。 在此控件的 MeasureOverride 可见性设置为在另一列中的 RepeatButton 上可见(RepeatButton 的可见性否则设置为折叠)。

这不会导致列扩展。仅当我调整窗口大小时才会发生这种情况。 一个简化的示例:

<DockPanel LastChildFill="True">
    <Grid DockPanel.Dock="Left">
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="TabItemColumn"/>
            <ColumnDefinition x:Name="ScrollRightColumn" Width="Auto"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer x:Name="PART_ScrollViewer" 
                Grid.Column="0" 
                Margin="-1,0,0,0" 
                Height="32" 
                CanContentScroll="True" 
                VerticalScrollBarVisibility="Hidden" 
                HorizontalScrollBarVisibility="Hidden" 
                HorizontalAlignment="Left">
            <local:TabPanel 
                    x:Name="tabPanel" 
                    HorizontalAlignment="Left" 
                    IsItemsHost="True" />
        </ScrollViewer>

        <RepeatButton Style="{StaticResource RepeatButtonScrollRight}"
            Visibility="{Binding ElementName=tabPanel, Path=CanScrollRight, Converter={StaticResource _localBooleanConverter}}"
            Grid.Column="1">
        </RepeatButton>

RepeatButton 的可见性被正确触发,据我所知它实际上已渲染,但包含列的 ActualWidth 在调整大小之前为零。

有什么想法吗?

I have a grid where one columnwidth is defined as *.
The other columns are defined as Auto.
The column with the *-definition contains a usercontrol derived from Panel that also implements IScrollInfo.
During this control's MeasureOverride visibility is set to visible on a RepeatButton in another column (the RepeatButton's visibility is otherwise set to collapsed).

This does not cause the column to expand. This will only occur when I resize my window.
A simplified example:

<DockPanel LastChildFill="True">
    <Grid DockPanel.Dock="Left">
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="TabItemColumn"/>
            <ColumnDefinition x:Name="ScrollRightColumn" Width="Auto"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer x:Name="PART_ScrollViewer" 
                Grid.Column="0" 
                Margin="-1,0,0,0" 
                Height="32" 
                CanContentScroll="True" 
                VerticalScrollBarVisibility="Hidden" 
                HorizontalScrollBarVisibility="Hidden" 
                HorizontalAlignment="Left">
            <local:TabPanel 
                    x:Name="tabPanel" 
                    HorizontalAlignment="Left" 
                    IsItemsHost="True" />
        </ScrollViewer>

        <RepeatButton Style="{StaticResource RepeatButtonScrollRight}"
            Visibility="{Binding ElementName=tabPanel, Path=CanScrollRight, Converter={StaticResource _localBooleanConverter}}"
            Grid.Column="1">
        </RepeatButton>

The visibility of the RepeatButton is triggered correctly, and as far as I can tell it is actually rendered, but the ActualWidth of the containing column is zero until resize.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2024-08-25 16:12:51

更改列宽后,您需要触发布局更改。在正确的父级上使用 InvalidateArrange()元素。注意避免无限循环。

You'll need to trigger a layout change after changing the column width. Use InvalidateArrange() on the proper parent element. Take care to avoid infinite cycles.

梦里兽 2024-08-25 16:12:51

InvalidateArrange 是一个很好的答案,但不幸的是,在实际安排控件时,它被默默地忽略了。所以诀窍是在排列完成后调用它。

这可能有效(我没有尝试过):

Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
  grid.InvalidateArrange();
}));

如果这不起作用,您可以尝试在 RepeatButton 上调用 InvalidateArrange 和/或 InvalidateMeasure,也在 Dispatcher.BeginInvoke 回调中调用。

InvalidateArrange is a good answer, but unfortunately it is silently ignored during the time a control is actually being arranged. So the trick is to call it after the arrange is complete.

This may work (I haven't tried it):

Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
  grid.InvalidateArrange();
}));

If this doesn't work, you might try calling InvalidateArrange and/or InvalidateMeasure on the RepeatButton, also within a Dispatcher.BeginInvoke callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文