c# - 带有 GridSplitter 的 WPF 扩展器

标签 c# .net wpf xaml

在我的 WPF 窗口 (.NET 4.0) 中,我有一个包含两列的网格:左侧的拉伸(stretch)文本框(或其他)和右侧的扩展器。同样在 Expander 中,我有 GridSplitter,它用于在展开 Expander 时调整左右列的大小。但它不起作用。

这是我的 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);
    }

最佳答案

您的网格分离器适用于内部网格(在扩展器中),而不适用于主网格。试试这个:

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

现在您需要处理当用户展开/折叠扩展器时最后一列发生的情况。

关于c# - 带有 GridSplitter 的 WPF 扩展器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19516904/

相关文章:

c# - WPF 中的比例最小宽度

c# - 如何在我的 ASP.NET MVC 应用程序中附加自定义成员资格提供程序?

c# - 当您声明一个没有实例的对象时,.Net 会做什么?

c# - 在大型数据集的可枚举 LINQ 查询结果上使用 ToList() - 效率问题?

C# 如何在类中使用 get、set 和使用枚举

c# - 为什么通用类型定义实现的接口(interface)会丢失类型信息?

c# - 切换时触发尺寸动画

wpf - 如何将列表框选定的项目内容绑定(bind)到文本框?

c# - C#中的method关键字是干什么用的?

c# - 更改标签的位置