网格不再调整大小

发布于 2024-07-12 10:34:26 字数 1840 浏览 1 评论 0原文

我将两个控件放在网格列中,并使第一个或第二个控件可见。 由于网格列的宽度设置为自动,它将根据可见子项的宽度调整自身大小...除非我使用网格拆分器更改列的宽度。
现在,如果我切换子控件的可见性,该列将不再根据其内容调整自身大小。 我怎样才能建立原来的行为?

谢谢!

编辑:
列的宽度属性是绑定到再次设置为 GridLength.Auto 的属性的数据(归功于 Martin Moser),但 Snoop 告诉我这将被忽略。 所以(新)问题是我如何将列的宽度设置回自动。

编辑:
示例

  • 启动应用程序
  • 使用复选框切换红色面板的可见性
  • => 正确调整第一列的大小
  • 使用拆分器调整列大小
  • 选中/取消选中复选框
  • => 不再调整第一列的大小

XAML:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300"
        Width="300">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="CONV_KEY"></BooleanToVisibilityConverter>
    </Window.Resources>
    <Grid ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Column="0" Width="100" Background="Red"
                Visibility="{Binding ElementName=_checkBox,Path=IsChecked,Converter={StaticResource CONV_KEY}}">
        </DockPanel>
        <DockPanel Grid.Column="0" Width="10" Background="Green" HorizontalAlignment="Left"></DockPanel>
        <GridSplitter Grid.Column="1" Width="10" ResizeBehavior="PreviousAndNext"></GridSplitter>
        <CheckBox x:Name="_checkBox" Grid.Column="3" VerticalAlignment="Top">Toggle</CheckBox>
    </Grid>
</Window>

I put two controls in a grid column and make either the first or the second control visible. Since the width of the grid column is set to auto it will resize itself according to the width of the visible child... unless I change the width of column using a grid splitter.
Now the column does not resize itself to its content anymore if I toggle the visibility of the child controls. How can I establish the original behavior?

Thanks!

EDIT:
The width property of the column is data bound to a property that is set to GridLength.Auto again (credits to Martin Moser), but Snoop tells me that this will be ignored. So the (new) question is how I can set the width of the column back to auto.

EDIT:
An example

  • Start the application
  • Use the checkbox to toggle the visibility of the red panel
  • => Correct resizing of the first column
  • Resize column with splitter
  • Check / uncheck checkbox
  • => No resizing of first column anymore

XAML:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300"
        Width="300">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="CONV_KEY"></BooleanToVisibilityConverter>
    </Window.Resources>
    <Grid ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Column="0" Width="100" Background="Red"
                Visibility="{Binding ElementName=_checkBox,Path=IsChecked,Converter={StaticResource CONV_KEY}}">
        </DockPanel>
        <DockPanel Grid.Column="0" Width="10" Background="Green" HorizontalAlignment="Left"></DockPanel>
        <GridSplitter Grid.Column="1" Width="10" ResizeBehavior="PreviousAndNext"></GridSplitter>
        <CheckBox x:Name="_checkBox" Grid.Column="3" VerticalAlignment="Top">Toggle</CheckBox>
    </Grid>
</Window>

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

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

发布评论

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

评论(2

清君侧 2024-07-19 10:34:26

当您需要这样做时,您是否尝试再次将列定义的宽度设置为 GridLength.Auto ?

did you try to set the Width of the columndefinition to GridLength.Auto again, when you need to do so?

怪我太投入 2024-07-19 10:34:26

这确实是一个黑客,但尝试将宽度设置为某个固定值,然后返回“自动”,这应该会触发更改,并且该值不应该被忽略,也许它正在工作......

It's quite a hack, but try to set the width to some fixed value, and then back to Auto, which should trigger a change, and the value shouldn't be ignored, maybe it's working...

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