Unable to set HorizontalContentAlignment in ListBox to "Stretch"

Mar 31, 2014 at 11:12 AM
Hello.

I am trying to set the HorizontalContentAlignment property of a ListBox to "Stretch", however the property appears to be overwritten.

The code I am using is the following:
<UserControl x:Class="ModernUITestApp2.Pages.Home"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
    <Style TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}" x:Key="Test1">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
    </UserControl.Resources>
    <Grid Style="{StaticResource ContentRoot}">
        <ListBox Margin="0,0,0,32" Style="{StaticResource ResourceKey=Test1}">
            <ListBoxItem>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="Text 1"/>
                    <TextBlock Grid.Column="1" Text="Text 2"/>
                </Grid>
            </ListBoxItem>
            <ListBoxItem Content="Item 2" />
            <ListBoxItem Content="Item 3" />
            <ListBoxItem Content="Item 4" />
            <ListBoxItem Content="Item 5" />
        </ListBox>
    </Grid>
</UserControl>
Could somebody please point me in the right direction.

Thanks.
Mar 31, 2014 at 11:29 AM
I was setting the properties of the wrong element. The element I want to modify is ListBoxItem, not ListBox