2013-03-27 67 views
1

嘿,我有3列擴展器,當你把它們全部打開並調整窗口大小時,它們就會適當地拉伸以適應窗口。當你關閉擴展器時,我希望它能在擴展器打開時縮小窗口的寬度。目前它將擴展器關閉到20像素,但是擴展器會更大(擴展到填充空白空間)。隨着擴展器關閉,縮小窗口大小

代碼:

<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> 

+0

你有沒有設置'SizeToContent =「WidthAndHeight」'對Window減去((Expander)sender).ActualWidth -20? – 2013-03-27 18:36:22

+0

是的,它被設置爲WidthAndHeight – Lainezor 2013-03-27 18:49:47

回答

3

附加一個事件處理程序擴展的倒塌事件,並從窗口寬度

10

設置窗口的SizeToContent"WidthAndHeight",並且不設置它的WidthHeight或designWidth或designHeight。

+0

當窗口打開並且其中的用戶控件寬度爲300時,第一個擴展器被展開。這個我不能改變。 – Lainezor 2013-03-27 19:00:05

+0

我認爲如果您發佈窗口的完整xaml代碼會更好。 – 2013-03-27 19:01:47

+0

發佈整個事情。 – Lainezor 2013-03-27 19:24:23