2011-11-22 40 views
1

我有以下情形在XAML:將網格與滾動查看器的內容對齊,而不是整個滾動查看器包括滾動條?

<Grid Grid.IsSharedSizeScope="True"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Grid x:Name="gridLabels" Grid.Column="0"> 
     <Grid.RowDefinitions> 
      <Row SharedSizeGroup="FirstRow" MinHeight=24/> 
      <Row SharedSizeGroup="SecondRow" MinHeight=24/> 
      <Row SharedSizeGroup="ThirdRow" MinHeight24/> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0">Top Item:</TextBlock> 
     <TextBlock Grid.Row="1">Middle Item:</TextBlock> 
     <TextBlock Grid.Row="2">Bottom Item:</TextBlock> 
    </Grid> 
    <ScrollViewer x:Name="scrVwItems" Grid.Column="1" VerticalScrollBarVisibility="Disabled" 
      HorizontalScrollBarVisibility="Visible" > 
     <ItemsControl ItemSource="{Binding Bondage}"> 
      <!--etc--> 
     </ItemsControl> 
    </ScrollViewer> 
</Grid> 

在ItemsControl的,DataTemplate d項目有自己的網格與SharedSizeGroup設定,使得TextBlock小號等排隊與TextBlock的標籤到最左邊。

我得到的問題是,直到ItemsControl填充SharedSizeGroup s不排隊,並且「Bottom Item:」文本與我的ScrollViewer的水平滾動條一起下降。

有沒有一種簡單的方法可以將「gridLabels」網格與ScrollViewer的內容區域對齊,而不是整個ScrollViewer本身?

回答

1

排序後,簡單地給ItemsControl(這是ScrollViewer的內容)一個名稱,並使用ElementName綁定到來自「gridLabels」.Height的ActualHeight。還將MinHeight應用於ItemsControl。

<Grid Grid.IsSharedSizeScope="True"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Grid x:Name="gridLabels" Grid.Column="0" 
     Height={Binding ElementName=myItemsControl, Path=ActualHeight}"> 
     <Grid.RowDefinitions> 
      <Row SharedSizeGroup="FirstRow" MinHeight=24/> 
      <Row SharedSizeGroup="SecondRow" MinHeight=24/> 
      <Row SharedSizeGroup="ThirdRow" MinHeight24/> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0">Top Item:</TextBlock> 
     <TextBlock Grid.Row="1">Middle Item:</TextBlock> 
     <TextBlock Grid.Row="2">Bottom Item:</TextBlock> 
    </Grid> 
    <ScrollViewer x:Name="scrVwItems" Grid.Column="1" VerticalScrollBarVisibility="Disabled" 
     HorizontalScrollBarVisibility="Visible" > 
     <ItemsControl ItemSource="{Binding Bondage}" x:Name="myItemsControl" MinHeight="150"> 
      <!--etc--> 
     </ItemsControl> 
    </ScrollViewer> 
</Grid>