折叠任何列后调整网格列的大小

时间:2016-02-10 20:45:45

标签: wpf

我在网格中有三个按钮。

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <Button Grid.Column="0" Content="A" />
  <Button Grid.Column="1" Content="B" />
  <Button Grid.Column="2" Content="C" />
</Grid>

我希望,具有可见性的按钮=&#34;折叠&#34;不得占用空间。

例如

(1)

<Button Grid.Column="0" Content="A" /> //33% of space
<Button Grid.Column="1" Content="B" /> //33% of space
<Button Grid.Column="2" Content="C" /> //33% of space

(2)

<Button Grid.Column="0" Content="A" /> //50% of space
<Button Grid.Column="1" Content="B" /> //50% of space
<Button Grid.Column="2" Content="C" Visibility="Collapsed" /> //0% of space

(3)

<Button Grid.Column="0" Content="A" /> //100% of space
<Button Grid.Column="1" Content="B" Visibility="Collapsed" /> //0% of space
<Button Grid.Column="2" Content="C" Visibility="Collapsed" /> //0% of space

我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个:

<Grid Name="grid">
    <Grid.Resources>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Collapsed">
                    <Setter Property="Tag" Value="0"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition  Width="{Binding ElementName=A,Path=Tag}" />
        <ColumnDefinition Width="{Binding ElementName=B,Path=Tag}"  />
        <ColumnDefinition  Width="{Binding ElementName=C,Path=Tag}"  />
    </Grid.ColumnDefinitions>
    <Button Grid.Column="0" Content="A" Name="A" Click="Button_Click" />
    <Button Grid.Column="1" Content="B"  Name="B"  />
    <Button Grid.Column="2" Content="C" Name="C" Visibility="Collapsed" />
</Grid>