2013-10-22 121 views
7

在我的WPF窗口(.NET 4.0)中,我有兩列的網格:左側的拉伸文本框(或其他)和右側的擴展器。同樣在Expander中,我有GridSplitter,當擴展器展開時,它打算調整左右列的大小。但它不起作用。WPF Expander與GridSplitter

這是我的XAML代碼:

<Window x:Class="WpfApplication10.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid ShowGridLines="True" > 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition Width="Auto" Name="column"/> 
    </Grid.ColumnDefinitions> 

    <TextBox Grid.Column="0" HorizontalAlignment="Stretch" TextWrapping="Wrap" 
      Text="TextBox" VerticalAlignment="Stretch" Background="Aqua"/> 

    <Expander Grid.Column="1" Header="Expander" ExpandDirection="Left" 
       HorizontalAlignment="Right" Background="LightBlue" > 
     <Expander.Content> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="5"/> 
        <ColumnDefinition Width="Auto"/> 
       </Grid.ColumnDefinitions> 

       <TextBlock Text="Some text Some text Some Text" Grid.Column="1"/> 
       <GridSplitter Grid.Column="0" Width="5"  
           ResizeBehavior="PreviousAndCurrent" 
           ResizeDirection="Columns" 
           HorizontalAlignment="Stretch"/> 
      </Grid> 
     </Expander.Content> 
    </Expander> 
</Grid></Window> 

合適的解決方案中找到。 XAML:

<Window x:Class="WpfApplication10.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow" Height="350" Width="525"> 

<Window.Resources> 
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/> 
</Window.Resources> 

<Grid ShowGridLines="True"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" Name="leftColumn"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="Auto" Name="rightColumn" /> 
    </Grid.ColumnDefinitions> 

    <TextBox Grid.Column="0" 
      HorizontalAlignment="Stretch" 
      TextWrapping="Wrap" 
      Text="TextBox" 
      VerticalAlignment="Stretch" 
      Background="Aqua" /> 

    <Expander Grid.Column="2" 
       Name="Expander" 
       Header="Expander" 
       ExpandDirection="Left" 
       Background="LightBlue" 
       Collapsed="Expander_Collapsed" 
       Expanded="Expander_Expanded" > 
     <TextBlock Text="Some text Some text Some Text" /> 
    </Expander> 
    <GridSplitter Grid.Column="1" 
        Width="5" 
        ResizeBehavior="PreviousAndNext" 
        ResizeDirection="Columns" 
        VerticalAlignment="Stretch" 
        Height="Auto" 
        Visibility="{Binding ElementName=Expander, Path=IsExpanded, 
           Converter={StaticResource BoolToVisConverter}}"/> 
</Grid></Window> 

代碼隱藏:

private void Expander_Collapsed(object sender, RoutedEventArgs e) 
    { 
     leftColumn.Width = new GridLength(1, GridUnitType.Star); 
     rightColumn.Width = new GridLength(1, GridUnitType.Auto); 
    } 

    private void Expander_Expanded(object sender, RoutedEventArgs e) 
    { 
     rightColumn.Width = new GridLength(1, GridUnitType.Star); 
    } 
+0

正是我需要感謝分享您的解決方案! – Mat

回答

4

網格分流作用於內網格(在擴展),而不是在主電網。試試這個:

<Window x:Class="WpfApplication10.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" 
     Height="350" 
     Width="525"> 
    <Grid ShowGridLines="True"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition Width="5" /> 
      <ColumnDefinition Width="Auto" 
           Name="column" /> 
     </Grid.ColumnDefinitions> 

     <TextBox Grid.Column="0" 
       HorizontalAlignment="Stretch" 
       TextWrapping="Wrap" 
       Text="TextBox" 
       VerticalAlignment="Stretch" 
       Background="Aqua" /> 

     <Expander Grid.Column="2" 
        Header="Expander" 
        ExpandDirection="Left" 
        Background="LightBlue"> 
      <TextBlock Text="Some text Some text Some Text" /> 
     </Expander> 
     <GridSplitter Grid.Column="1" 
         Width="5" 
         ResizeBehavior="PreviousAndNext" 
         ResizeDirection="Columns" 
         VerticalAlignment="Stretch" 
         Height="Auto" /> 
    </Grid> 
</Window> 

現在你需要處理當用戶展開/摺疊擴展器時最後一列會發生什麼。

+0

我正在尋找沒有代碼隱藏或自定義轉換器的答案:AFAIK如果擴展器位於左側,則可以僅使用XAML代碼。但你的答案足夠好。我將發佈我的代碼。 –

0

試試這個,如果它有助於解決您的問題。

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid ShowGridLines="True" > 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition Width="Auto" Name="column"/> 

    </Grid.ColumnDefinitions> 

    <TextBox Grid.Column="0" HorizontalAlignment="Stretch" TextWrapping="Wrap" 
     Text="TextBox" VerticalAlignment="Stretch"/> 

    <Expander Grid.Column="1" Header="Expander" ExpandDirection="Left" 
      HorizontalAlignment="Right"> 
     <Expander.Content> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="5"/> 
        <ColumnDefinition Width="Auto"/> 
       </Grid.ColumnDefinitions> 

       <TextBlock Text="Some text Some text Some Text" Grid.Column="0"/> 
       <GridSplitter Grid.Column="1" Width="5"  
          ResizeBehavior="PreviousAndCurrent" 
          ResizeDirection="Columns" 
          HorizontalAlignment="Stretch"/> 
      </Grid> 
     </Expander.Content> 
    </Expander> 
</Grid> 

+1

不起作用... –