当前位置:   article > 正文

【C#/WPF】GridSplitter 分割布局,拖拽控件分隔栏以改变控件尺寸

xaml中gridsplitter 多行多列 单独改变大小

需求:界面由多部分控件组成,想要拖拽控件之间的分隔栏以改变尺寸。

MainWindow.xaml:

  1. <Grid>
  2. <Grid.ColumnDefinitions>
  3. <ColumnDefinition Width="300"/>
  4. <ColumnDefinition Width="300"/>
  5. </Grid.ColumnDefinitions>
  6. <ListBox Grid.Row="0" Grid.Column="0">
  7. <ListBoxItem>aaaa</ListBoxItem>
  8. <ListBoxItem>bbbb</ListBoxItem>
  9. <ListBoxItem>cccb</ListBoxItem>
  10. </ListBox>
  11. <TextBlock Grid.Row="0" Grid.Column="1">sadfas</TextBlock>
  12. <GridSplitter Grid.Row="0" Grid.Column="0" Width="0" VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
  13. </Grid>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

运行效果如下:

红色虚线表示了拖动分隔栏以改变控件的尺寸边缘。

分隔栏往左拖动

分隔栏往右拖动

坑点:

  • 必须使用Grid及其行和列来定义,否则能显示但无法拖动。例如下面的写法是无效的:
  1. <StackPanel Orientation="Horizontal">
  2. <TextBlock Text="LLLLLLLLLLLLLL"/>
  3. <GridSplitter Width="10" HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Wheat"/>
  4. <TextBlock Text="RRRRRRRRRRRRRR"/>
  5. </StackPanel>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 对于左右分割列,必须指定分隔栏GridSplitter的宽度Width,以及设置VerticalAlignment,否则无效。
  • 对于上下分割行,必须指定分隔栏GridSplitter的高度Height,以及设置HorizontalAlignment,否则无效。

再来一个详细一点的例子:

  1. <Window x:Class="WpfApplication1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfApplication1"
  7. xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
  8. xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
  9. mc:Ignorable="d"
  10. Title="MainWindow" Height="600" Width="1000">
  11. <Grid>
  12. <Grid.RowDefinitions/>
  13. <Grid.ColumnDefinitions>
  14. <ColumnDefinition/>
  15. <ColumnDefinition Width="10"/>
  16. <ColumnDefinition/>
  17. </Grid.ColumnDefinitions>
  18. <TextBlock Grid.Row="0" Grid.Column="0" Text="LLLLLLLLLLLLLL"/>
  19. <GridSplitter Grid.Row="0" Grid.Column="1" Width="10" HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Wheat"/>
  20. <Grid Grid.Row="0" Grid.Column="2">
  21. <Grid.RowDefinitions>
  22. <RowDefinition/>
  23. <RowDefinition Height="10"/>
  24. <RowDefinition/>
  25. </Grid.RowDefinitions>
  26. <Grid.ColumnDefinitions>
  27. <ColumnDefinition/>
  28. </Grid.ColumnDefinitions>
  29. <TextBlock Grid.Row="0" Grid.Column="0" Text="RRRRRRTTTTTTTT"/>
  30. <GridSplitter Grid.Row="1" Grid.Column="0" Height="10" HorizontalAlignment="Stretch" Background="Wheat"/>
  31. <TextBlock Grid.Row="2" Grid.Column="0" Text="RRRRRRBBBBBBBB"/>
  32. </Grid>
  33. </Grid>
  34. </Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

运行效果如图:下面的水平和垂直的黄色分隔栏都是可以拖动的。
这里写图片描述

重要的参考:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/511829
推荐阅读
相关标签
  

闽ICP备14008679号