2011-08-26 8 views
7

मुझे क्या करना चाहते हैं: मैं अपने सिल्वरलाइट 4 आवेदन जो सामग्री के साथ एक साथ ऊंचाई में बढ़ता में एक scrollview करना चाहते हैं, लेकिन शो करता है एक स्क्रॉलबार अगर यह अन्यथा इसके कंटेनर से अधिक तेज़ हो जाएगा।बढ़ते scrollview (ऊंचाई = "ऑटो" maxHeight = "मांसपेशियों")

समाधान मैंने पाया: मैं पूछे जाने वाले प्रश्नों के बहुत सारे मिल गया जहां समाधान ScrollViewer strech के लिए गया था, लेकिन वह नहीं है कि मैं क्या चाहता हूँ। स्क्रॉलव्यूवर हमेशा जितना संभव हो उतना छोटा होना चाहिए।

मेरे समस्या: उसे कुछ अतिरिक्त ScrollViewer एक हैडर स्थित है जो स्थिर ऊंचाई के साथ एक StackPanel है की चोटी पर मुश्किल बनाने के लिए।

समाधान दृष्टिकोण 1: मैंने इसे सादा एक्सएएमएल के साथ पहली बार कोशिश की लेकिन मुझे पता नहीं लगा कि इसे कैसे काम करना चाहिए।

<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: तब मैं ScrollViewer चारों ओर * अधिकतम * एक StackPanel युक्त कंटेनर के actualHeight साथ ScrollViewer की ऊंचाई, इसलिए मैं inseted स्थापित करने के लिए कोशिश की। यह वीएस -2010 एक्सएएमएल डिजाइनर में काम करता है लेकिन कोड निष्पादन पर नहीं। मुझे कोई सुराग नहीं है ...

<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 के उचित उपयोग में है। आप ग्रिड पंक्ति चाहते हैं जिसमें स्क्रॉलव्यू शेष स्थान का आकार हो। आप नहीं चाहते कि स्क्रॉलव्यूअर प्रारंभ में उस स्थान पर कब्जा कर ले, लेकिन उसे उस स्थान तक ही सीमित होना चाहिए। Stretch का डिफ़ॉल्ट VerticalAlignment सभी उपलब्ध आकार पर कब्जा कर लेगा। इसके बजाए 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> 

हालांकि ध्यान दें VerticalScrollBarVisibility जो आप अपने पाठ कि यह होना चाहिए में संकेत के बावजूद अपनी खुद की XAML में शामिल नहीं हैं। शायद वास्तव में यह वही है जो आप वास्तव में सभी के साथ हैं। उस जगह के साथ VerticalAlignment हटा दें। आपके बाकी परिदृश्य के आधार पर आप पाएंगे कि वास्तव में स्क्रॉलव्यूअर की पूर्ण सीमा की रूपरेखा सीमा अधिक समझ में आता है।

+1

अच्छा बढ़िया, यह काम करता है। और आपकी व्याख्या मेरे लिए कुल संवेदना बनाती है! आपका बहुत बहुत धन्यवाद! – heynest