2015-04-06 42 views
0

我是一個新手在wpf,我試圖創建兩個擴展器,包含每個擴展器上的listview,在我創建一個搜索文本框並工作正常。 問題是,我不能創建滾動視圖到兩個擴展器,並適合父級控制的堆疊面板高度。 如何創建滾動視圖到列表視圖沒有文本框?自動調整大小的父母身高與列表視圖內滾動視圖

代碼:

<StackPanel Orientation="Vertical"> 
    <TextBox Margin="3,3,3,3" FontSize="16" Height="25" Name="searchTextBox" TextChanged="SearchTextBox_OnTextChanged" Grid.Row="0"></TextBox> 
    <Border CornerRadius="6" BorderBrush="Gray" BorderThickness="1" DockPanel.Dock="Top" Grid.Row="1"> 
     <StackPanel Orientation="Vertical"> 
      <Expander IsExpanded="True" Background="LightGray" Grid.Row="0" > 
       <Expander.Header> 
         <TextBlock Text="A" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" /> 
       </Expander.Header> 
       <Expander.Content> 
        <ListView Name="RecentEngines" BorderThickness="0" > 
         <ListView.ItemTemplate> 
          <DataTemplate> 
           <TextBlock Text="{Binding Name}" FontWeight="Bold" /> 
          </DataTemplate> 
         </ListView.ItemTemplate> 
        </ListView> 
       </Expander.Content> 
      </Expander> 

      <Expander IsExpanded="True" Background="LightGray" Grid.Row="1"> 
       <Expander.Header> 
        <TextBlock Text="B" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" /> 
       </Expander.Header> 
       <Expander.Content> 

        <ListView Name="Engines" BorderThickness="0" MaxHeight="300"> 
         <ListView.ItemTemplate> 
          <DataTemplate> 
           <TextBlock Text="{Binding Name}" FontWeight="Bold" /> 
          </DataTemplate> 
         </ListView.ItemTemplate> 
        </ListView> 

       </Expander.Content> 
      </Expander> 

     </StackPanel> 
    </Border> 

</StackPanel> 
+0

StackPanels對於佈局而言有點噁心。一般而言,從長遠來看,使用Grid更容易。在第一個StackPanel的父結構中可以實現嗎? – goobering

+0

你的意思是用一個網格來包裝外部堆棧面板? – user1940350

+0

對不起,這是措辭不佳。完全刪除堆疊面板並將其替換爲單列網格,每個項目包含在其自己的行中。那麼你有更好的選擇設置個人的高度和寬度,而不是讓堆疊面板做任何他們想要的。 – goobering

回答

0

每有關問題的評論這可能是更容易一些使用網格,而不是StackPanels完成。

<Grid x:Name="LayoutRoot"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <TextBox Name="searchTextBox" 
      Grid.Row="0" 
      Height="25" 
      Margin="3,3,3,3" 
      FontSize="16" /> 
    <Border Grid.Row="1" 
     BorderBrush="Gray" 
     BorderThickness="1" 
     CornerRadius="6"> 
     <ScrollViewer Height="300"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 

       <Expander Grid.Row="0" 
         Background="LightGray" 
         IsExpanded="True"> 
        <Expander.Header> 
         <TextBlock VerticalAlignment="Bottom" 
           FontSize="22" 
           FontWeight="Bold" 
           Foreground="Gray" 
           Text="A" /> 
        </Expander.Header> 
        <Expander.Content> 
         <ListView Name="RecentEngines" BorderThickness="0"> 
          <ListView.ItemTemplate> 
           <DataTemplate> 
            <TextBlock FontWeight="Bold" Text="{Binding Name}" /> 
           </DataTemplate> 
          </ListView.ItemTemplate> 
         </ListView> 
        </Expander.Content> 
       </Expander> 

       <Expander Grid.Row="1" 
         Background="LightGray" 
         IsExpanded="True"> 
        <Expander.Header> 
         <TextBlock VerticalAlignment="Bottom" 
           FontSize="22" 
           FontWeight="Bold" 
           Foreground="Gray" 
           Text="B" /> 
        </Expander.Header> 
        <Expander.Content> 
         <ListView Name="Engines" 
           MaxHeight="300" 
           BorderThickness="0"> 
          <ListView.ItemTemplate> 
           <DataTemplate> 
            <TextBlock FontWeight="Bold" Text="{Binding Name}" /> 
           </DataTemplate> 
          </ListView.ItemTemplate> 
         </ListView> 

        </Expander.Content> 
       </Expander> 

      </Grid> 
     </ScrollViewer> 
    </Border> 
</Grid> 

還有一些冗餘的標記元素,我拉了。你想盡量避免這種情況,因爲它可以對深度嵌套的元素造成嚴重破壞。這裏的'Grid.Row'元素是多餘的,因爲它在一個StackPanel內部:

<Expander IsExpanded="True" Background="LightGray" Grid.Row="0" > 
相關問題