2016-10-17 13 views
0

UWPでリストボックスを水平方向と垂直方向に伸ばしたいと思っています。私はいくつかのWPFソリューションを試しましたが、それらのどれも働いていませんでした。Strech ListBox/ItemsControl in UWP

<Page.Content> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <ListBox VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" Background="Green"> 
      <ListBox.ItemContainerStyle> 
       <Style TargetType="ListBoxItem"> 
        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter> 
        <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter> 
        <Setter Property="VerticalAlignment" Value="Stretch"></Setter> 
        <Setter Property="HorizontalAlignment" Value="Stretch"></Setter> 
        <Setter Property="Background" Value="AliceBlue" /> 
       </Style> 
      </ListBox.ItemContainerStyle> 
      <ListBox.Template> 
       <ControlTemplate TargetType="ListBox"> 
        <ItemsPresenter Height="252" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> 
       </ControlTemplate> 
      </ListBox.Template> 
      <ListBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" /> 
       </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
      <ListBoxItem>asdf</ListBoxItem> 
      <ListBoxItem>asdfasdf</ListBoxItem> 
      <ListBoxItem>asdfsdf</ListBoxItem> 
      <ListBoxItem>34</ListBoxItem> 
      <ListBoxItem>as2df</ListBoxItem> 
      <ListBoxItem>asdf</ListBoxItem> 
     </ListBox> 
    </Grid> 
</Page.Content> 

結果は以下の通りです:

私はUWPにリストボックスを伸ばすことができますどのように

enter image description here

?私が試した何(Stretch line to width of Itemstemplate canvas in itemscontrol

答えて

1

あなたは明示的にHeight="252"を設定しています。それが現れないのはその理由です。また、実際のListBoxの背景をGreenに設定すると、ItemsPanelTemplateによって上書きされるので、Greenは表示されません。

最終的なXAMLは以下のようになります。

<ListBox VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" Background="Green"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter> 
      <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter> 
      <Setter Property="VerticalAlignment" Value="Stretch"></Setter> 
      <Setter Property="HorizontalAlignment" Value="Stretch"></Setter> 
      <Setter Property="Background" Value="AliceBlue" /> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBoxItem>asdf</ListBoxItem> 
    <ListBoxItem>asdfasdf</ListBoxItem> 
    <ListBoxItem>asdfsdf</ListBoxItem> 
    <ListBoxItem>34</ListBoxItem> 
    <ListBoxItem>as2df</ListBoxItem> 
    <ListBoxItem>asdf</ListBoxItem> 
</ListBox> 

これはテストされていませんが、期待通りに動作するはずです。

+1

Horizo​​ntalAlignmentとVerticalAlignmentは必要ありません。 –