Question

In my Windows Phone application I'm using the DockPanel to align two buttons: one on the left side of the screen (PanoramaItem), second - on the right. This code works well:

<controls:PanoramaItem Header="page1">
   <panel:DockPanel>
      <Button  Content="Right"  panel:DockPanel.Dock="Right"/>
      <Button  Content="Left"  panel:DockPanel.Dock="Left"/>
   </panel:DockPanel>
</controls:PanoramaItem>

But if I want dock buttons this way in ListBox - both align to the left :(

<controls:PanoramaItem Header="page2">
   <ListBox Margin="0,0,-12,0" HorizontalContentAlignment="Stretch" 
        ItemsSource="{Binding Collection}" Height="418" VerticalAlignment="Top">
     <ListBox.ItemTemplate>
        <DataTemplate>
           <panel:DockPanel >
              <Button  Content="Right"  panel:DockPanel.Dock="Right"/>
              <Button  Content="Left"  panel:DockPanel.Dock="Left"/>
           </panel:DockPanel>
        </DataTemplate>
     </ListBox.ItemTemplate>
  </ListBox>
</controls:PanoramaItem>
Was it helpful?

Solution

Add this inside the tag for list box:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment"
                Value="Stretch" />
    </Style>
</ListBox.ItemContainerStyle>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top