ListBox の ItemTemplate を ListBox の幅全体に水平に伸ばすにはどうすればよいでしょうか? 質問する

ListBox の ItemTemplate を ListBox の幅全体に水平に伸ばすにはどうすればよいでしょうか? 質問する

ListItems のオレンジ色の背景をリストボックスの全幅に拡張したいと思います。

現時点では、FirstName + LastName の幅のみとなります。

可能な限りすべての要素を Horizo​​ntalAlignment="Stretch" に設定しました。

ユーザーがリストボックスを拡大すると、リストボックスアイテムの背景も拡大するようにしたいので、絶対値を入力したくありません。

ListBoxItems の背景色が ListBox の幅いっぱいになるようにするには、何をする必要がありますか?

<Window x:Class="TestListBoxSelectedItemStyle.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestListBoxSelectedItemStyle"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <local:CustomerViewModel x:Key="TheDataProvider"/>

        <DataTemplate x:Key="CustomerItemTemplate">
            <Border CornerRadius="5" Background="Orange" HorizontalAlignment="Stretch" Padding="5" Margin="3">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Width="Auto">
                    <TextBlock HorizontalAlignment="Stretch">
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} {1}">
                            <Binding Path="FirstName"/>
                            <Binding Path="LastName"/>
                        </MultiBinding>
                    </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
            </Border>
        </DataTemplate>

    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Path=GetAllCustomers, Source={StaticResource TheDataProvider}}"
                 ItemTemplate="{StaticResource CustomerItemTemplate}"/>
    </Grid>
</Window>

ベストアンサー1

私は見つけたここに別の解決策があります両方の投稿に遭遇したので...

これはマイルズの回答からの抜粋です:

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

これは私にとってはうまくいきました。

おすすめ記事