2011-08-26 4 views
7

내가 원하는 무엇 : 내가 내용과 함께 높이에서 자랍니다 내 실버 라이트 4 응용 프로그램에서있는 ScrollView을하고 싶은, 하지만 쇼를 않습니다 그렇지 않으면 스크롤 막대가 컨테이너보다 더 많이 자랄 수 있습니다.성장있는 ScrollView (높이 = "자동"MaxHeight입니다 = "스트레치")

솔루션은 내가 발견 솔루션이에서 ScrollViewer를 스트레치을 어디에 내가 묻는 질문을 많이 찾았지만 그게 내가 원하는 하지입니다. Scrollviewer는 항상 가능한 한 작아야합니다.

내 문제 : 스크롤 뷰어 위에 조금 더 어렵게 만들려면 정적 높이가있는 스택 패널 인 헤더가 있어야합니다.

솔루션 승인 1 : 먼저 일반 XAML로 시도했지만 작동 방법을 파악할 수 없습니다.

<Grid Height="Auto" x:Name="myGrid" > 
    <Grid.RowDefinitions> 
     <RowDefinition Height="100"/> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 

    <StackPanel Grid.Row="0" Background="AliceBlue"> 
     <!-- Dummy Header--> 
    </StackPanel> 

    <ScrollViewer Grid.Row="1" Height="Auto"> 
     <Button Width="100" Height="50" Click="Button_Click" /> 
     <!-- onClick the button will switch between height="600" and height="50" 
      Code: 
      private void Button_Click(object sender, RoutedEventArgs e) 
      { 
       if (sender is Button) 
       { 
        Button btn = (Button)sender; 
        btn.Height = (btn.Height == 50) ? 600 : 50 ; 
       } 
      } 
     --> 
    </ScrollViewer> 
</Grid> 

버튼을 클릭하면 키가 커지기 때문에 버튼이 높아지면 스크롤바가 잘립니다. 어떤 설교?

해결 접근 2 : 그러면 I은 주위의 ScrollViewer * 최대 * 포함하는 컨테이너의 ActualHeight와의 ScrollViewer의 높이, 따라서 I는 inseted StackPanel에 설정하려고. VS2010 XAML 디자이너에서는 작동하지만 코드 실행에서는 작동하지 않습니다. 나는 아무런 단서가 없다 ...

<Grid Height="Auto" x:Name="myGrid" > 
    <Grid.RowDefinitions> 
     <RowDefinition Height="100"/> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 

    <StackPanel Grid.Row="0" Background="AliceBlue"> 
     <!-- Dummy Header--> 
    </StackPanel> 
    <StackPanel Grid.Row="1" x:Name="myStackPanel" Height="Auto" VerticalAlignment="Stretch"> 
     <ScrollViewer Height="Auto" MaxHeight="{Binding ElementName=myStackPanel, Path=ActualHeight}"> 
      <Button Width="100" Height="50" Click="Button_Click" /> 
      <!-- onClick the button will switch between height="600" and height="50" 
       Code: 
       private void Button_Click(object sender, RoutedEventArgs e) 
       { 
        if (sender is Button) 
        { 
         Button btn = (Button)sender; 
         btn.Height = (btn.Height == 50) ? 600 : 50 ; 
        } 
       } 
      --> 
     </ScrollViewer> 
    </StackPanel> 
</Grid> 

미리 감사드립니다!

답변

6

용액은 VerticalAlignment을 적절히 사용한다. scrollview가 포함 된 Grid 행을 나머지 공간의 크기로 만듭니다. Scrollviewer가 처음에는 모든 공간을 차지하지는 않지만 그 공간으로 제한해야합니다. 기본 VerticalAlignmentStretch 인 경우 사용 가능한 모든 크기를 차지합니다. 대신 Top을 사용하면 그리드가 크기를 사용 가능한 공간으로 제한 할 때까지 필요한만큼만 높게됩니다.

<Grid x:Name="myGrid" > 
    <Grid.RowDefinitions> 
     <RowDefinition Height="100"/> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <StackPanel Grid.Row="0" Background="AliceBlue"> 
     <!-- Dummy Header--> 
    </StackPanel> 
    <ScrollViewer Grid.Row="1" VerticalAlignment="Top" VerticalScrollBarVisibility="Auto"> 
    </ScrollViewer> 
</Grid> 

그러나주의 당신이해야 텍스트의 표시에도 불구하고 자신의 XAML에 포함하지 않는 VerticalScrollBarVisibility. 사실 이것은 아마도 당신이 정말로 함께있는 것입니다. 그 자리에 VerticalAlignment을 제거하십시오. 나머지 시나리오에 따라 실제로 스크롤 뷰어의 전체 범위에 대한 윤곽선 테두리가 더 적합하다는 것을 알 수 있습니다.

+1

잘 작동합니다. 그리고 당신의 설명은 내게 총을 의미합니다! 고마워요! – heynest