Reputation: 59
This is the structure of the program:
- window
- styles
- togglebutton
- textblock
- grid
Each row in the grid will be a toggle button and when we click the togglebutton the textblock expands and when we click it again the textblock collapses.
The problem is when it expands, the window increases in size but when it collapses it won't reduce the size to the rows size. What to do for this? Can anyone help?
I tried setting the windows sizetocontent property to width and height and it is not the problem. I think there is something to do with the grid and not the window.
Upvotes: 0
Views: 1005
Reputation: 5122
I think you were in the right way, using SizeToContent
property of the window!
Try this code (declare the Converter and change references!):
C# IValueConverter
for the visibility of the children
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 code for your window
<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>
Upvotes: 1