Get ListBox to resize with window, but not resize with content

旧巷老猫 提交于 2021-02-08 12:24:11

问题


There must be an elegant solution to this problem, but I can't find anything online. I have a grid that has one column and row with width/height *, containing a ListBox. I have my Window SizeToContents set to WidthAndHeight to allow the window to resize to the proper size for each set of UI widgets/fonts. When I add items to the ListBox, it resizes, causing the window to grow.

I want the ListBox to resize if I change the size of the window, but if I add content that is longer than the width of the ListBox, I want the scrollbar to appear and not for it to grow, causing the Window to grow. If I set explicit sizes for the Window and set SizeToContent to Manual (the default), it works as I intend.

Is there any way to size the window to the contents at startup and continue to have the ListBox grow with the window size, but not with its content?


回答1:


Following Potecaru Tudor's suggestion, I solved a similar issue by wrapping the ListBox in another container and binding the ListBox's Height to the ActualHeight of the container.

Here is a XAML code snippet to help:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Border x:Name="HeightHelperPanel" Grid.Row="0">
        <ListBox ItemsSource="{Binding Path=FileNameCollection}"
                 MinHeight="60"
                 Height="{Binding Path=ActualHeight, ElementName=HeightHelperPanel}"/>
    </Border>
</Grid>

Basically, the Border container doesn't grow when its content grows, but it will stay stretched to the height of the grid row. The grid row will fill up any space the containing window will allow it, but will not want to force any height constraints on the window. The ListBox's height is therefore constrained to the height of the Border.

Hope that helps anyone else who has had this somewhat frustrating issue.




回答2:


HorizontalAlignment="Stretch" VerticalAlignment="Stretch"




回答3:


This is the intended behavior resulted from setting the SizeToContent property to WidthAndHeight as described here.

What you might do is binding the Width and the Height of the ListBox to the ActualWidth and ActualHeight properties of its container, or use a converter and bind them directly to the Window's respective properties.




回答4:


I hope you have the below requirement, 1) ListBox should make use of scroll bar it its content's size grows bigger than its original.

2) If window is resized, ListBox should grow/shrink along with Window.

I have tried the same with a simple example, please check this and if it differs from your requirement, let me know,

XAML code:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Text="I am in Main Grid"/>
        <ListBox Grid.Row="1" BorderBrush="BlueViolet" BorderThickness="5" Margin="10">
            <TextBlock Text="I am a ListBox"/>
            <Button Content="Add Height and Width of ListBox by 100 pixels" Click="Button_Click"/>
            <ListBoxItem Content="ListBoxItem" Background="AliceBlue" Margin="10" BorderBrush="Blue" Width="{Binding ListBoxWidth}" Height="{Binding ListBoxHeight}"/>
        </ListBox>
    </Grid>
</Window>

C# code:

public partial class MainWindow : Window,INotifyPropertyChanged
{
    private int m_ListBoxWidth = 350;

    public int ListBoxWidth
    {
        get { return m_ListBoxWidth; }
        set 
        {
            m_ListBoxWidth = value;
            OnPropertyChanged("ListBoxWidth");
        }
    }

    private int m_ListBoxHeight = 150;

    public int ListBoxHeight
    {
        get { return m_ListBoxHeight; }
        set 
        {
            m_ListBoxHeight = value; 
            OnPropertyChanged("ListBoxHeight"); 
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ListBoxWidth += 190;
        ListBoxHeight += 140;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



回答5:


To make ListBox to resize when you change the size of the window just use:

<ListBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" .../>

To enable scrolling in your ListBox you can find an answer here because the default template of the ListBox include a ScrollViewer.

How can I get a vertical scrollbar in my ListBox?



来源:https://stackoverflow.com/questions/9200406/get-listbox-to-resize-with-window-but-not-resize-with-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!