2016-10-18 18 views
1

ListBoxをListBoxItemでストレッチしたいと思います。 ListBox自体をストレッチすることは問題ではありません。問題は、リストボックスの空き領域を使用するようにListBoxItemに指示することです。UWP内のStrech ListBoxとListBoxItems

<Page.Content> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Height="200"> 
     <ListBox VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" Background="Green" ItemsSource="{x:Bind Path=ChessFieldList}" > 
      <ListBox.ItemTemplate> 
       <DataTemplate > 
        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Yellow" BorderBrush="Red" BorderThickness="3" > 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Page.Content> 

以下のイメージは、上記と予想される結果を示しています。

enter image description here

どのように私は期待される結果を達成することができますか?

[編集]他と私の意見では、正しい解決策:Set Width and Height of ItemsControl Children

+0

[Strech ListBox/ItemsControl in UWPの可能な複製](0120-338-501) – AVK

+0

リストボックスのストレッチに関することはありません - これはストリートリストボックスアイテムに関するものです – Briefkasten

答えて

1

これは共通の問題です。あなたがやらなければならないことは、あまりにもListBoxItem秒のHorizontalContentAlignmentに設定されます。

<ListBox Background="Green" ItemsSource="{x:Bind ChessFieldList}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Background="Yellow" BorderBrush="Red" BorderThickness="3" Height="50"> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

StackPanelは、任意のコンテンツが含まれていないので、それは任意の高さを持っていないので、私はそれにHeight="50"を単に追加しましたこのデモンストレーションの目的。

<ListView.GroupStyle> 
    <GroupStyle HidesIfEmpty="False"> 
     <GroupStyle.HeaderContainerStyle> 
      <Style TargetType="ListViewHeaderItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
       <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
      </Style> 
     </GroupStyle.HeaderContainerStyle> 
    </GroupStyle> 
</ListView.GroupStyle> 

をそれはあなたが尋ねたよりも少しですが、それはあまり知られて技術です:

+0

私はStackpanelをGridに置き換えようとしました。そのため、Heightの明示的な値は必要ありません。しかし、それはうまくいかないようです。高さを設定しないと解決策はありませんか? – Briefkasten

+1

@Briefkasten子要素のないグリッドは、高さがゼロです。私のDataTemplateとして使用 – Clemens

+0

@Clemens <グリッド背景= "黄" BorderBrush = "赤" BorderThickness = "3"> <ボーダー背景= "ベージュ" CornerRadius = "20"> Briefkasten

0

単にあなたがグループ化を使用する場合は、あなたが含まれている必要があり

<ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    </Style> 
</ListBox.ItemContainerStyle> 

を追加します。

関連する問題