WPF 中的网格拆分器问题

发布于 2024-08-11 15:43:41 字数 2089 浏览 4 评论 0原文

我想要一个像 VS 2008 这样的布局。其中我想要两列,第二列再次分成两列。

我在下面提到的 xaml 中完成了此操作,但是 GridSplitter 垂直不可见(它分割了两列)。

我希望 GridSplitter 都可以调整大小。一个 GridSplitter 调整左侧窗格和右侧窗格的大小,另一个 GridSplitter 调整子网格顶部窗格和右侧窗格的大小。

第二个 GridSplitter 正在工作通过此 XAML,但我无法生成拆分右侧窗格和左侧窗格的 XAML 代码。 请帮助!

<Window x:Class="AlarmUI_2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <StackPanel Background="Aqua" Grid.Column="0" >
            <TextBlock FontSize="35" Foreground="#58290A" 
                       TextWrapping="Wrap">Left Hand Side</TextBlock>
        </StackPanel>
        <GridSplitter Grid.Column="0" ResizeDirection="Auto" 
                      Grid.RowSpan="1" 
                      HorizontalAlignment="Stretch" 
                      VerticalAlignment="Center"/>
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>            
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <ListBox Grid.Row="0" Background="Red">
                <ListBoxItem>Hello</ListBoxItem>
                <ListBoxItem>World</ListBoxItem>
            </ListBox>
            <GridSplitter Grid.Row="1" Height="5" Background="Gray"
                          VerticalAlignment="Top" HorizontalAlignment="Stretch" />
            <ListBox Grid.Row="1" Background="Violet" Margin="0,5,0,0">
                <ListBoxItem>Hello</ListBoxItem>
                <ListBoxItem>World</ListBoxItem>
            </ListBox>
        </Grid>
    </Grid>
</Window>

I want a layout like VS 2008. In which I want two columns and second columns is again split into two.

I done that in the xaml mentioned below, but the GridSplitter is not visible vertically ( which split two columns).

I want both the GridSplitters to be resizable. One GridSplitter resizes the Left Hand Pane and Right Hand Pane and another GridSplitter resizes the subgrid's top pane and right pane..

The Second GridSplitter is working through this XAML but I am not able to produce XAML code that Splits the Right hand Pane and Left hand pane..
Pleas Help!!

<Window x:Class="AlarmUI_2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <StackPanel Background="Aqua" Grid.Column="0" >
            <TextBlock FontSize="35" Foreground="#58290A" 
                       TextWrapping="Wrap">Left Hand Side</TextBlock>
        </StackPanel>
        <GridSplitter Grid.Column="0" ResizeDirection="Auto" 
                      Grid.RowSpan="1" 
                      HorizontalAlignment="Stretch" 
                      VerticalAlignment="Center"/>
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>            
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <ListBox Grid.Row="0" Background="Red">
                <ListBoxItem>Hello</ListBoxItem>
                <ListBoxItem>World</ListBoxItem>
            </ListBox>
            <GridSplitter Grid.Row="1" Height="5" Background="Gray"
                          VerticalAlignment="Top" HorizontalAlignment="Stretch" />
            <ListBox Grid.Row="1" Background="Violet" Margin="0,5,0,0">
                <ListBoxItem>Hello</ListBoxItem>
                <ListBoxItem>World</ListBoxItem>
            </ListBox>
        </Grid>
    </Grid>
</Window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情魔剑神 2024-08-18 15:43:41

将垂直 Splitter 更改为

<GridSplitter Grid.Column="0" Width="5" ResizeDirection="Auto" 
            Grid.RowSpan="1" 
            HorizontalAlignment="Right" 
            VerticalAlignment="Stretch"/>

这将是使用 GridSplitter 的更好方法

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="5"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <StackPanel Background="Aqua" Grid.Column="0" >
        <TextBlock FontSize="35" Foreground="#58290A" 
           TextWrapping="Wrap">Left Hand Side</TextBlock>


    </StackPanel>

    <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch"/>

    <Grid Grid.Column="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="5" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0" Background="Red">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>World</ListBoxItem>
        </ListBox>
        <GridSplitter Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch"/>
        <ListBox Grid.Row="2" Background="Violet">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>World</ListBoxItem>
        </ListBox>
    </Grid>
</Grid>

Change your vertical Splitter to

<GridSplitter Grid.Column="0" Width="5" ResizeDirection="Auto" 
            Grid.RowSpan="1" 
            HorizontalAlignment="Right" 
            VerticalAlignment="Stretch"/>

This will be much better way to use GridSplitter

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="5"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <StackPanel Background="Aqua" Grid.Column="0" >
        <TextBlock FontSize="35" Foreground="#58290A" 
           TextWrapping="Wrap">Left Hand Side</TextBlock>


    </StackPanel>

    <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch"/>

    <Grid Grid.Column="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="5" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0" Background="Red">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>World</ListBoxItem>
        </ListBox>
        <GridSplitter Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch"/>
        <ListBox Grid.Row="2" Background="Violet">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>World</ListBoxItem>
        </ListBox>
    </Grid>
</Grid>
看海 2024-08-18 15:43:41

GridSplitters 可能应该位于网格中自己的行/列上,而不是与其他控件共享单元格。

The GridSplitters should probably be on their own row/column in the grid, not sharing a cell with the other controls.

难得心□动 2024-08-18 15:43:41

您的网格分割器位于其他控件的后面,这就是您看不到它的原因。您可以将其移至 XAML 文件的底部(以便最后添加)或使用 Panel.ZIndex 附加属性。此外,您必须正确设置分割器的宽度:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <StackPanel Background="Aqua" Grid.Column="0" >
        <TextBlock FontSize="35" Foreground="#58290A" 
           TextWrapping="Wrap">Left Hand Side</TextBlock>
    </StackPanel>
    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0" Background="Red">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>World</ListBoxItem>
        </ListBox>
        <GridSplitter Grid.Row="1" Height="5" Background="Gray"
              VerticalAlignment="Top" HorizontalAlignment="Stretch" />
        <ListBox Grid.Row="1" Background="Violet" Margin="0,5,0,0">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>World</ListBoxItem>
        </ListBox>
    </Grid>
    <GridSplitter Grid.Column="0" ResizeDirection="Columns" 
            Grid.RowSpan="1" Width="5"
            HorizontalAlignment="Right"/>
</Grid>

Your gridsplitter is behind the other controls that's why you can't see it. You can either move it to the bottom in your XAML file (so it is added last) or use the Panel.ZIndex attached property. Additionally you have to set the width for the splitter correctly correctly:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <StackPanel Background="Aqua" Grid.Column="0" >
        <TextBlock FontSize="35" Foreground="#58290A" 
           TextWrapping="Wrap">Left Hand Side</TextBlock>
    </StackPanel>
    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0" Background="Red">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>World</ListBoxItem>
        </ListBox>
        <GridSplitter Grid.Row="1" Height="5" Background="Gray"
              VerticalAlignment="Top" HorizontalAlignment="Stretch" />
        <ListBox Grid.Row="1" Background="Violet" Margin="0,5,0,0">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>World</ListBoxItem>
        </ListBox>
    </Grid>
    <GridSplitter Grid.Column="0" ResizeDirection="Columns" 
            Grid.RowSpan="1" Width="5"
            HorizontalAlignment="Right"/>
</Grid>
浮生未歇 2024-08-18 15:43:41

我已经实现了该功能,XAML如下:

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <StackPanel Background="Aqua" Grid.Column="0" >
        <TextBlock FontSize="35" Foreground="#58290A" 
               TextWrapping="Wrap">Left Hand Side</TextBlock>


    </StackPanel>


    <GridSplitter HorizontalAlignment="Right" ResizeDirection="Columns"  Width="5" />

    <Grid Grid.Column="1">
        <Grid.RowDefinitions>            
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>




    <ListBox Grid.Row="0" Background="Red">
        <ListBoxItem>Hello</ListBoxItem>
        <ListBoxItem>World</ListBoxItem>
    </ListBox>
    <GridSplitter Grid.Row="1" Height="5" Background="Gray"
                  VerticalAlignment="Top" HorizontalAlignment="Stretch" />
    <ListBox Grid.Row="1" Background="Violet" Margin="0,5,0,0">
        <ListBoxItem>Hello</ListBoxItem>
        <ListBoxItem>World</ListBoxItem>
    </ListBox>
        </Grid>
</Grid>

I have acheived the functionality, the XAML is mentioned below:

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <StackPanel Background="Aqua" Grid.Column="0" >
        <TextBlock FontSize="35" Foreground="#58290A" 
               TextWrapping="Wrap">Left Hand Side</TextBlock>


    </StackPanel>


    <GridSplitter HorizontalAlignment="Right" ResizeDirection="Columns"  Width="5" />

    <Grid Grid.Column="1">
        <Grid.RowDefinitions>            
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>




    <ListBox Grid.Row="0" Background="Red">
        <ListBoxItem>Hello</ListBoxItem>
        <ListBoxItem>World</ListBoxItem>
    </ListBox>
    <GridSplitter Grid.Row="1" Height="5" Background="Gray"
                  VerticalAlignment="Top" HorizontalAlignment="Stretch" />
    <ListBox Grid.Row="1" Background="Violet" Margin="0,5,0,0">
        <ListBoxItem>Hello</ListBoxItem>
        <ListBoxItem>World</ListBoxItem>
    </ListBox>
        </Grid>
</Grid>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文