C#WPF Grid / GridSplitter

时间:2013-11-23 10:54:25

标签: c# wpf layout

我想要的是:

  • 一个包含3列的Grid-Splitter
  • 所有3列的MinWidth应该可以正常工作
  • 左侧和右侧列应具有“defaultSize”,如100和200
  • 当我调整表单大小时,只有中间的列应该调整大小。左侧和右侧的列应保持相同的大小
  • 如果我向表单添加控件,我希望他们根据拆分器
  • 更改其大小

我有什么:

  • 我有一个包含3列的网格
  • 所有3列的MinWidth都有效,但只能使用ColumnWidth =“*”
  • 我不知道如何为左右列设置“DefaultSize”,因为如果我更改ColumnWidth,则MinWidth不再起作用
  • 当我调整表单大小时,所有3列都会改变它们的大小,而不仅仅是中间的列

                               

    <ToolBarTray DockPanel.Dock="Top" IsLocked="True" Background="Pink">
        <ToolBar Height="26" Background="Beige">
        </ToolBar>
    </ToolBarTray>
    
    <StatusBar DockPanel.Dock="Bottom" Height="23" Background="Orange"/>
    
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="50"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width ="*" MinWidth="100"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*" MinWidth="50"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    
        <GridSplitter Grid.Column="1" Width="5" ShowsPreview="True" Background="Red" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
    
    
        <GridSplitter Grid.Column="3" Width="5" ShowsPreview="True" Background="Red" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
    
    </Grid>
    

到目前为止感谢:)

1 个答案:

答案 0 :(得分:1)

设置左列和右列的宽度(默认大小),它应该是工作

<Grid>
  <StatusBar Height="23" Background="Orange" DockPanel.Dock="Bottom"/>
  <Grid>
     <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="50" Width="200"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition MinWidth="100" Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition MinWidth="50" Width="200"/>
     </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
     </Grid.RowDefinitions>
     <GridSplitter
        Width="5"
        Grid.Column="1"
        HorizontalAlignment="Center"
        VerticalAlignment="Stretch"
        Background="Red"
        ShowsPreview="True"/>
     <GridSplitter
        Width="5"
        Grid.Column="3"
        HorizontalAlignment="Center"
        VerticalAlignment="Stretch"
        Background="Red"
        ShowsPreview="True"/>
  </Grid>
</Grid>

修改

这是针对第一个网格最大化问题的修复

<Grid x:Name="Root">
      <Grid.Resources>
        <local:MaxWidthConverter x:Key="MaxWidthConverter" />
      </Grid.Resources>

      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition MinWidth="50"
                            Width="200"
                            MaxWidth="{Binding ElementName=Root, Path=ActualWidth, Mode=OneWay, Converter={StaticResource MaxWidthConverter}, ConverterParameter=260}" />
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition MinWidth="200"
                            Width="*" />
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition MinWidth="50"
                            Width="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <GridSplitter Width="5"
                      Grid.Column="1"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Stretch"
                      Background="Red"
                      ShowsPreview="True" />
        <GridSplitter Width="5"
                      Grid.Column="3"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Stretch"
                      Background="Red"
                      ShowsPreview="True" />
      </Grid>
    </Grid>

简单转换器计算第一列的最大宽度(260 = 200 minwidth + 50 minwidth + 5 splitter width + 5 splitter width

public class MaxWidthConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    double minWidth;
    if (value is double && parameter is string && double.TryParse((string)parameter, NumberStyles.Any, CultureInfo.InvariantCulture, out minWidth)) {
      var maxWidth = (double)value - minWidth;
      return maxWidth < 0 ? 0 : maxWidth;
    }
    return 0;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    return DependencyProperty.UnsetValue;
  }
}

enter image description here

enter image description here