wpf 扩展器关闭时缩小窗口大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15666746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shrinking the window size as expander closes
提问by Lainezor
Hey guys I have 3 column expanders, when you have them all open and resize the window they stretch properly to fit the window. When you an close expander, I want it to shrink the window by the width of that expander when it was open. It currently closes the expander back down to 20pixels but the open expanders will be larger (it stretches to fill in the empty space).
嘿伙计们,我有 3 个列扩展器,当您将它们全部打开并调整窗口大小时,它们会正确拉伸以适合窗口。当您关闭扩展器时,我希望它在打开时将窗口缩小为该扩展器的宽度。它目前将扩展器关闭回 20 像素,但打开的扩展器会更大(它会伸展以填充空白空间)。
Code:
代码:
<UserControls:ExpanderWindow x:Class="Lainezor.Windows.ExpanderWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xt="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended"
xmlns:pg="clr-namespace:Microsoft.Windows.Controls.PropertyGrid;assembly=WPFToolkit.Extended"
xmlns:MyUserControls="clr-namespace:Lainezor.UserControls.ExpanderControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
SizeToContent="WidthAndHeight" Background="AliceBlue"
ResizeMode="CanResize" WindowStartupLocation="Manual" Loaded="Window_Loaded">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition>
<ColumnDefinition.Style>
<Style TargetType="ColumnDefinition">
<Setter Property="Width" Value="20" />
<Setter Property="MinWidth" Value="20" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsExpanded, ElementName=Expander1}" Value="True">
<Setter Property="Width" Value="20*" />
</DataTrigger>
</Style.Triggers>
</Style>
</ColumnDefinition.Style>
</ColumnDefinition>
<ColumnDefinition Width="2"/>
<ColumnDefinition>
<ColumnDefinition.Style>
<Style TargetType="ColumnDefinition">
<Setter Property="Width" Value="20" />
<Setter Property="MinWidth" Value="20" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsExpanded, ElementName=Expander2}" Value="True">
<Setter Property="Width" Value="20*" />
</DataTrigger>
</Style.Triggers>
</Style>
</ColumnDefinition.Style>
</ColumnDefinition>
<ColumnDefinition Width="2"/>
<ColumnDefinition>
<ColumnDefinition.Style>
<Style TargetType="ColumnDefinition">
<Setter Property="Width" Value="20" />
<Setter Property="MinWidth" Value="20" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsExpanded, ElementName=Expander3}" Value="True">
<Setter Property="Width" Value="20*" />
</DataTrigger>
</Style.Triggers>
</Style>
</ColumnDefinition.Style>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Expander Name="Expander1" Grid.Column="0" IsExpanded="True" IsEnabled="True" ExpandDirection="Right">
<Expander.Header>
<TextBlock Text="{Binding Source={StaticResource ResourceWrapper}, Path=MyStrings.Expander1Label}" RenderTransformOrigin="0,0" >
<TextBlock.LayoutTransform>
<RotateTransform Angle="90"/>
</TextBlock.LayoutTransform>
</TextBlock>
</Expander.Header>
<MyUserControls:UserControl1 x:Name="UserControl1" SizeChanged="UserControl1_SizeChanged"/>
</Expander>
<GridSplitter x:Name="gridSplitter" Grid.Column="1" Width="2" Background="#CBE5FE" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext" VerticalAlignment="Stretch" />
<Expander Grid.Column="2" Grid.RowSpan="2" Name="Expander2" ExpandDirection="Right" Grid.IsSharedSizeScope="True">
<Expander.Header >
<TextBlock Text="{Binding Source={StaticResource ResourceWrapper}, Path=MyStrings.Expander2Label}">
<TextBlock.LayoutTransform>
<RotateTransform Angle="90" />
</TextBlock.LayoutTransform>
</TextBlock>
</Expander.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<MyUserControls:UserControl2 x:Name="UserControl2" HorizontalAlignment="Stretch"/>
</Grid>
</Expander>
<GridSplitter x:Name="gridSplitterTwo" Grid.Column="3" Width="2" Background="#CBE5FE" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext" VerticalAlignment="Stretch" />
<Expander Grid.Column="4" Grid.RowSpan="2" Name="Expander3" ExpandDirection="Right">
<Expander.Header >
<TextBlock Text="Lainezor">
<TextBlock.LayoutTransform>
<RotateTransform Angle="90" />
</TextBlock.LayoutTransform>
</TextBlock>
</Expander.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<MyUserControls:UserControl3 x:Name="UserControl3" HorizontalAlignment="Stretch" />
</Grid>
</Expander>
</Grid>
回答by Hossein Narimani Rad
Set the SizeToContentof the Window to "WidthAndHeight"and do not set it's Widthor Heightor designWidth or designHeight.
将SizeToContentWindow设置为"WidthAndHeight"而不是将其设置为WidthorHeight或 designWidth 或 designHeight。
回答by Darren
Attach an event handler to the collapsed event of the expander and subtract the ((Expander)sender).ActualWidth -20from the window width
将事件处理程序附加到扩展器的折叠事件并((Expander)sender).ActualWidth -20从窗口宽度中减去

