2016-09-25 79 views
1

它似乎適用於所有人,但不適合我。我想在我的窗口較大時用水平填充ListView項目,然後用項目堆疊面板。我有以下的.xaml填寫WPF中的ListView項目

<ListView x:Name="comboBox" ItemsSource="{Binding ArticleCategories}" SelectedItem="{Binding SelectedArticleCategory}" HorizontalAlignment="Stretch" Margin="0,0,0,0" Background="Azure" VerticalAlignment="Top"> 
     <ListView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" /> 
      </ItemsPanelTemplate> 
     </ListView.ItemsPanel> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" ></Setter> 
      </Style> 
     </ListView.ItemContainerStyle> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <Button Content="abc" Width="Auto"></Button> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

這裏是我有什麼 enter image description here

+0

目前還不清楚你的實際問題是什麼。你想要物品均勻地填充StackPanel,還是隻需要最後一個物品填充重新渲染空間? – Clemens

+0

如果窗口較大,我希望物品均勻地填充StackPanel –

+0

@AshotKhachatryan。如果窗戶更小,該怎麼辦? – dkozl

回答

1

您可以使用UniformGrid有單列爲ItemsPanel

<ListView ...> 
    <ListView.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Rows="1"/> 
     </ItemsPanelTemplate> 
    </ListView.ItemsPanel> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
     </Style> 
    </ListView.ItemContainerStyle> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Button Content="abc"/> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView>