2013-05-29 105 views
2

enter image description hereGridSplitter - 無限寬度

簡單GridSplitter在某種意義上,當我將其向左超越MinWidth舉止異常,另一列無限擴大。我在這裏錯過了什麼?

<Grid> 
    <Grid x:Name="holdergrid" HorizontalAlignment="Stretch">   
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width ="*" MinWidth="300"/>     
      <ColumnDefinition Width ="425" MinWidth="300"/> 
     </Grid.ColumnDefinitions> 
     <Button Grid.Column="0" Content="Left"></Button> 
     <Button Grid.Column="1" Content="Right"></Button> 
     <GridSplitter Name="GridSplitterFolders" HorizontalAlignment="Left" Grid.Column ="1" Width ="10" ResizeBehavior="PreviousAndCurrent" /> 
    </Grid> 
</Grid> 

回答

1

添加一個寬度設置爲Auto的ColumnDefinition以容納GridSplitter本身並將ResizeBehavior更改爲PreviousAndNext。

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width ="*" MinWidth="300"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width ="425" MinWidth="300"/> 
    </Grid.ColumnDefinitions> 
    <Label Content="Left" Grid.Column="0" /> 
    <GridSplitter HorizontalAlignment="Right" 
       VerticalAlignment="Stretch" 
       Grid.Column="1" ResizeBehavior="PreviousAndNext" 
       Width="5" Background="#FFBCBCBC"/> 
    <Label Content="Right" Grid.Column="2" /> 
</Grid> 
+0

這可以起作用,但試着將分離器移動超過極限......現在,當您再次嘗試移動分離器時,它一開始就不會先拖動之前的移動距離。這看起來像一個錯誤。 –

0

在我看來,這是一個錯誤發生時,至少有一列定義設置爲像素值。如果您將列定義更改爲星號值,則一切正常。檢查你的代碼的這個修改後的版本:

<Window x:Class="Gridsplitter.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
     </Grid.RowDefinitions> 
     <Grid x:Name="Holdergrid" MaxWidth="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=ActualWidth}"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*" MinWidth="300" /> 
       <ColumnDefinition Width="Auto" /> 
       <ColumnDefinition Width="400" MinWidth="300" /> 
      </Grid.ColumnDefinitions> 
      <Button Grid.Column="0" Content="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"></Button> 
      <Button Grid.Column="2" Content="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"></Button> 
      <GridSplitter Name="GridSplitterFolders" HorizontalAlignment="Center" VerticalAlignment="Stretch" Grid.Column ="1" Width ="5" ResizeBehavior="PreviousAndNext"/> 
     </Grid> 
     <StackPanel Grid.Row="1" Orientation="Horizontal"> 
      <TextBlock Text="{Binding ElementName=Holdergrid, Path=ActualWidth, StringFormat=Actual Width: {0}}"></TextBlock> 
      <TextBlock Text="{Binding ElementName=Holdergrid, Path=Width, StringFormat=Width: {0}}" Margin="10 0 0 0"></TextBlock> 
      <TextBlock Text="{Binding ElementName=Holdergrid, Path=MaxWidth, StringFormat=Max Width: {0}}" Margin="10 0 0 0"></TextBlock> 
     </StackPanel> 
    </Grid> 
</Window> 

當你拖動網格分流到左側,右列將有所無限增長,甚至超過我限制了MaxWidth。如果在第三列定義中將Width="400"替換爲Width="*",則它可以正常工作。顯然,這是實現它的唯一方法。

+0

如果您只使用星號或自動網格長度的列定義,則可以刪除網格上的MaxWidth限制。 – feO2x

1

feO2x是對的。 GridSplitter混合*和像素值時會出現無限寬度。

水平GridSplitter的

正常工作,例如應該有:

<Grid.ColumnDefinitions> 
     <ColumnDefinition Width="3*" MinWidth="200"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="1*" MinWidth="200"/> 
    </Grid.ColumnDefinitions> 

而且

<GridSplitter Grid.Column="1" Width="6" Style="{StaticResource gridSplitterStyleVertical}" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Stretch" 
       ResizeBehavior="PreviousAndNext" 
       ResizeDirection="Columns"> 

通知

HorizontalAlignment="Center" 

ITIS也是很重要的有這樣才能使其正常工作