11

我正在将 WinForms 应用程序迁移到 WPF。到目前为止,一切都进展顺利,除了我尝试使用 GridSplitter 时,我永远无法在运行时调整任何东西的大小。

为了确保它不仅仅是我的代码,我尝试从 LearnWPF.com 编译GridSplitter 示例,但它似乎也不起作用。当我将鼠标悬停在不会发生的拆分器上时,我希望看到标准的调整大小光标,并且据我所知,窗口中也没有拆分器的其他视觉表示。

我在这里想念什么?

<Window x:Class="UI.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" Height="300" Width="300">
<Grid>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <StackPanel Background="#feca00" Grid.Column="0">
            <TextBlock FontSize="35" Foreground="#58290A"
               TextWrapping="Wrap">Left Hand Side</TextBlock>
        </StackPanel>
        <GridSplitter/>
        <Border CornerRadius="10" BorderBrush="#58290A"
          BorderThickness="5" Grid.Column="1">
            <TextBlock FontSize="25" Margin="20" Foreground="#FECA00"
               TextWrapping="Wrap">Right Hand Side</TextBlock>
        </Border>
    </Grid>

4

2 回答 2

13

在您的示例中,GridSplitter被放置在第一列。我不记得我的 WPF 对齐规则离开了我的头顶,但我认为它可能被放置在第一列的左侧。不是你想要的。

GridSplitter让控件占据一行或一列比尝试与其他控件共享一行或一列要容易得多。

<Window x:Class="UI.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" Height="300" Width="300">
<Grid>
      <Grid>
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
         </Grid.ColumnDefinitions>
         <StackPanel Grid.Column="0" Background="#feca00">
            <TextBlock FontSize="35" Foreground="#58290A" TextWrapping="Wrap">
              Left Hand Side
            </TextBlock>
         </StackPanel>
         <GridSplitter
            Width="4"
            Grid.Column="1"
            Background="Red"
            VerticalAlignment="Stretch"
            HorizontalAlignment="Center"/>
         <Border
            Grid.Column="2"
            BorderBrush="#58290A"
            BorderThickness="5"
            CornerRadius="10">
            <TextBlock FontSize="25" Foreground="#FECA00" TextWrapping="Wrap">
              Right Hand Side
            </TextBlock>
         </Border>
      </Grid>
   </Grid>
</Window>
于 2010-06-01T21:35:29.670 回答
-1

您缺少 Z-Ordering 的重要概念。控件按照您列出它们的顺序放置在 z 顺序中。基本上,您的网格拆分器被最后一列覆盖。如果您将网格拆分器放在 z 顺序的最后一列上,它应该可以正常工作而无需额外的列:

<Window x:Class="UI.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" Height="300" Width="300">
    <Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <StackPanel Background="#feca00" Grid.Column="0">
                <TextBlock FontSize="35" Foreground="#58290A" TextWrapping="Wrap">Left Hand Side</TextBlock>
            </StackPanel>
            <Border CornerRadius="10" BorderBrush="#58290A" BorderThickness="5" Grid.Column="1">
                <TextBlock FontSize="25" Margin="20" Foreground="#FECA00" TextWrapping="Wrap">Right Hand Side</TextBlock>
            </Border>
            <GridSplitter Grid.Column="1"/>
        </Grid>
    </Grid>
于 2017-03-27T00:36:30.733 回答