How to set width to 100% in WPF

后端 未结 2 1864
感情败类
感情败类 2020-12-23 09:25

Is there any way how to tell component in WPF to take 100% of available space?

Like

width: 100%;  

in CSS

相关标签:
2条回答
  • 2020-12-23 09:36

    It is the container of the Grid that is imposing on its width. In this case, that's a ListBoxItem, which is left-aligned by default. You can set it to stretch as follows:

    <ListBox>
        <!-- other XAML omitted, you just need to add the following bit -->
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
    
    0 讨论(0)
  • 2020-12-23 09:48

    You could use HorizontalContentAlignment="Stretch" as follows:

    <ListBox HorizontalContentAlignment="Stretch"/>
    
    0 讨论(0)
提交回复
热议问题