Grid Splitter problem in WPF

▼魔方 西西 提交于 2019-12-20 18:35:55

问题


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>

回答1:


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>



回答2:


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




回答3:


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>



回答4:


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>



来源:https://stackoverflow.com/questions/1748168/grid-splitter-problem-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!