代码之家  ›  专栏  ›  技术社区  ›  Crippledsmurf

为什么我的网格拆分器根本不工作?

  •  10
  • Crippledsmurf  · 技术社区  · 14 年前

    我正在将WinForms应用程序迁移到WPF。到目前为止,一切都进展顺利,除了我尝试使用GridSplitter之外,在运行时我无法对任何内容进行调整。

    为了确保它不仅仅是我的代码,我试图编译 GridSplitter sample 在learnwpf.com上,它似乎也不起作用。当我将鼠标移到拆分器上时,我希望看到标准的调整大小光标,而这是不会发生的,而且据我所见,窗口中也没有拆分器的其他视觉表示。

    我这里缺什么?

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

    2 回复  |  直到 7 年前
        1
  •  12
  •   moswald Jarod42    14 年前

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

    做一个 网格分离器 占用行或列,而不是尝试与其他控件共享行或列。

    <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>
    
        2
  •  -1
  •   Quark Soup    7 年前

    你错过了z排序的重要概念。控件按您列出的顺序以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>