简体   繁体   English

使用GridSplitter的WPF扩展器

[英]WPF Expander with GridSplitter

In my WPF window (.NET 4.0) I have Grid with two columns: stretched textbox (or whatever) on the left side and Expander on the right. 在我的WPF窗口(.NET 4.0)中,我有两列Grid:左侧是拉伸文本框(或其他),右侧是Expander。 Also in Expander I have GridSplitter, which is intended to resize both left and right columns when Expander is expanded. 同样在Expander中我有GridSplitter,它旨在扩展Expander时调整左右列的大小。 But it doesn't work. 但它不起作用。

This is my XAML code: 这是我的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>

Appropriate solution is found. 找到了合适的解决方案。 XAML: 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>

Code-behind: 代码隐藏:

    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);
    }

Your grid splitter works on the inner grid (in expander) and not on the main grid. 您的网格分割器在内部网格(在扩展器中)而不是在主网格上工作。 Try this: 试试这个:

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

Now you'd need to handle what happens to the last column when the user expands/collapses the expander. 现在,您需要处理用户展开/折叠扩展器时最后一列发生的情况。

Try this, if it helps to resolve your problem. 试试这个,如果它有助于解决您的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM