2013-01-07 5 views
1

은 내가 GridSplitterGridSplitter 자동 크기 조정

간단한 예제 코드에 문제가있어 :

<Grid x:Name="MainGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="100"></RowDefinition> 
     <RowDefinition Height="2"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
    </Grid.RowDefinitions> 
    <Grid x:Name="TopGrid" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Red"/> 
    <GridSplitter ResizeDirection="Rows" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="2" Background="Black" /> 
    <Grid x:Name="BottomGrid" Grid.Row="2" HorizontalAlignment="Stretch" Background="Aquamarine" VerticalAlignment="Stretch"/> 
</Grid> 

이 두 개의 그리드가 수직 GridSplitter로 구분 만듭니다.

달성하고자하는 것은 GridSplitter이 Grid의 컨텐츠에 자동 정렬된다는 것입니다. 예를 들어, 맨 아래 Grid에 접을 수있는 요소가있는 경우, 요소를 축소하면 맨 위 Grid가 커지 길 원합니다. 확장하면 상단의 Grid이 작아집니다.

어떻게하면됩니까? 나중에 나는 3 GridSplitter의 4 Grids을 가질 것입니다 ... 그래서 솔루션은 복수 GridSplitter과 함께 작동해야합니다.

[편집] :

내 XAML은 :

<Grid x:Name="MainGrid"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="100"> 
      <RowDefinition.MinHeight> 
       <MultiBinding Converter="{StaticResource ResourceKey=MultiValueConverter}"> 
        <Binding ElementName="dgStapelliste" Path="ActualHeight"></Binding> 
       </MultiBinding> 
      </RowDefinition.MinHeight> 
     </RowDefinition> 
     <RowDefinition Height="1"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition >   
    </Grid.RowDefinitions> 

    <Grid Grid.Row="0" Grid.Column="0" x:Name="Test"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="200"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <WPF:DataGrid GridHeaderContent="{Binding StapelListe.HeaderText}" SelectedItem="{Binding StapelListe.SelectedItem}" Grid.Row="0" Grid.Column="0" x:Name="dgStapelliste" HorizontalAlignment="Stretch" ItemsSource="{Binding StapelListe, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/> 
     <WPF:DataGrid GridHeaderContent="{Binding EinzelListe.HeaderText}" Grid.Row="0" Grid.Column="1" x:Name="dgEinzelliste" HorizontalAlignment="Stretch" ItemsSource="{Binding EinzelListe, Mode=OneWay}"/> 

     <GridSplitter Grid.Column="0" Width="1" VerticalAlignment="Stretch" Background="Black" /> 
    </Grid> 

    <Grid Grid.Row="2" Grid.Column="0" Grid.RowSpan="2"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

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

     <WPF:DataGrid GridHeaderContent="{Binding Anforderungsliste.HeaderText}" Grid.Column="0" x:Name="dgAnforderungsliste" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Anforderungsliste, Mode=OneWay}"/> 

     <GridSplitter Grid.Column="0" Width="1" VerticalAlignment="Stretch" Background="Black" /> 
    </Grid> 
    <GridSplitter Grid.Column="0" Grid.Row="1" Height="1" ResizeDirection="Rows" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Black" x:Name="testSplitter" /> 
</Grid> 

답변

3

당신은 당신이 원하는 것을 달성하기 위해 MultiBinding을 사용할 수 있습니다. 각 RowDefinition를 들어

당신과 같이 그 내용의 ActualHeight 년대에 바인딩의 minHeight 설정 : 그것의 ActualHeight 변경

  <RowDefinition Height="100"> 
       <RowDefinition.MinHeight> 
        <MultiBinding Converter="{StaticResource ResourceKey=CalcAll}"> 
         <Binding ElementName="firstElementInThisRow" Path="ActualHeight"></Binding> 
         <Binding ElementName="secondElementInThisRow" Path="ActualHeight"></Binding> 
         <Binding ElementName="thirdElementInThisRow" Path="ActualHeight"></Binding> 
         <Binding ElementName="fourthElementInThisRow" Path="ActualHeight"></Binding> 
        </MultiBinding> 
       </RowDefinition.MinHeight> 
      </RowDefinition> 

귀하의 변환기

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     double result = 0.0; 
     foreach (object item in values) 
     { 
      result += System.Convert.ToDouble(item); 
     } 
     return result; 
    } 

    public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture) 
    { 
     return null; 
    } 

매번 같을 수는 컨트롤을 확장하고 Binding은 부모 RowDefinition의 Update -> MinHeight를 가져옵니다.

그러나 VerticalAlignment to Stretch를 제어 할 수없는 경우에는 ActualHeight가 확장으로 변경되지 않기 때문에 하나를 설정할 수 없습니다.

편집 : 내가 지금 생각할 수있는 유일한 속성은 DesiredSize.Height 재산권이기 때문에합니다 (DesiredSize.Height-값이 변경되면, 바인딩 늘 갱신), 당신은 바인딩을 사용할 수 없습니다. 그러나 당신이 속성을 사용할 수 있습니다 pherhaps는 세터 년대하여 PropertyChanged 이벤트를 발생 첫 번째 행의 minHeight (각 행에 대해 하나 개의 속성)에 바인딩 double 형의 (호출 MinHeightRowOne입니다 수 있습니다) :

public double _minHeightRowOne; 
    public double MinHeightRowOne 
    { 
     get 
     { 
      return _minHeightRowOne; 
     } 
     set 
     { 
      _minHeightRowOne = value; 
      OnPropertyChanged("MinHeightRowOne"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string name) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
     } 
    } 


지금
<RowDefinition Height="100" MinHeight="{Binding Path=MinHeightRowOne}"/> 

(각 행에 대해 하나의 처리기)의 첫 번째 행에있는 모든 제어하여 SizeChanged 이벤트이 이벤트 핸들러를 추가

private List<KeyValuePair<string,double>> oldVals = new List<KeyValuePair<string,double>>(); 

    private void ElementInRowOneSizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     FrameworkElement elem = (FrameworkElement)sender; 
     MinHeightRowOne -= oldVals.Find(kvp => kvp.Key == elem.Name).Value; 
     elem.Measure(new Size(int.MaxValue, int.MaxValue)); 
     MinHeightRowOne += elem.DesiredSize.Height; 
     oldVals.Remove(oldVals.Find(kvp => kvp.Key == elem.Name)); 
     oldVals.Add(new KeyValuePair<string, double>(elem.Name, elem.DesiredSize.Height)); 
    } 

이것을 통해 MinHeight of the Rows는 컨트롤의 크기가 변경 될 때마다 업데이트됩니다 (항목 확장 또는 축소 포함).

이 컨트롤을 만들려면 컨트롤마다 고유 한 이름이 있어야합니다.

+0

이 솔루션의 문제점은 "ActualHeight"가 항상 행의 높이라는 것입니다. 행의 높이를 "100"으로 설정하면이 행의 DataGrid의 ActualHeight도 "100"이됩니다.그것은 스크롤 가능한 내용을 합산하지 않습니다 –

+0

질문에 xaml을 추가 할 수 있습니까? –

+0

그래, 내가 편집 했어 –