儿童崩溃后网格不会缩小尺寸

时间:2012-08-22 10:02:26

标签: wpf

这是该计划的结构:

- window
  - styles
    - togglebutton
    - textblock
  - grid

网格中的每一行都是一个切换按钮,当我们点击togglebutton时,文本块会展开,当我们再次单击它时,文本块会折叠。

问题是当它扩展时,窗口的大小会增加,但是当它崩溃时,它不会将大小减小到行大小。该怎么办?有人可以帮忙吗?

我尝试将windows sizetocontent属性设置为width和height,这不是问题所在。我认为网格与窗口有关。

1 个答案:

答案 0 :(得分:1)

我认为你是以正确的方式,使用窗口的SizeToContent属性!

尝试此代码(声明转换器并更改引用!):

C#IValueConverter了解儿童的可见度

public sealed class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool)
        {
            if ((bool)value)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }
        else
            throw new NotImplementedException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

窗口的XAML代码

<Window x:Class="TestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converters="clr-namespace:TestWpf.Converters"
        VerticalAlignment="Stretch"
        SizeToContent="WidthAndHeight"
        HorizontalContentAlignment="Stretch"
        VerticalContentAlignment="Stretch"
        Title="MainWindow">
<Window.Resources>
    <ResourceDictionary>
        <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <!--Row 1-->
    <StackPanel Grid.Row="0"
                Orientation="Vertical">
        <ToggleButton Content="expand/collapse"
                      x:Name="Button1" />
        <TextBlock FontSize="30"
                   HorizontalAlignment="Center"
                   Visibility="{Binding Path=IsChecked, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}, ElementName=Button1}"
                   Text="Sample Text 1" />
    </StackPanel>
    <!--Row 2-->
    <StackPanel Grid.Row="1"
                Orientation="Vertical">
        <ToggleButton Content="expand/collapse"
                      x:Name="Button2" />
        <TextBlock FontSize="30"
                   HorizontalAlignment="Center"
                   Visibility="{Binding Path=IsChecked, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}, ElementName=Button2}"
                   Text="Sample Text 2" />
    </StackPanel>
    <!--Row 3-->
    <StackPanel Grid.Row="2"
                Orientation="Vertical">
        <ToggleButton Content="expand/collapse"
                      x:Name="Button3" />
        <TextBlock FontSize="30"
                   HorizontalAlignment="Center"
                   Visibility="{Binding Path=IsChecked, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}, ElementName=Button3}"
                   Text="Sample Text 3" />
    </StackPanel>
    </Grid>
</Window>