获取 ListBox 随窗口调整大小,但不随内容调整大小

发布于 2025-01-03 09:10:20 字数 365 浏览 3 评论 0原文

这个问题一定有一个优雅的解决方案,但我在网上找不到任何东西。我有一个网格,其中有一列和一行,宽度/高度为 *,包含一个列表框。我将 Window SizeToContents 设置为 WidthAndHeight,以允许窗口大小调整为每组 UI 小部件/字体的正确大小。当我向列表框添加项目时,它会调整大小,导致窗口变大。

如果我更改窗口的大小,我希望列表框能够调整大小,但是如果我添加的内容长于列表框的宽度,我希望滚动条出现而不是让它增长,从而导致窗口增长。如果我为窗口设置显式大小并将 SizeToContent 设置为 Manual(默认值),它就会按我的预期工作。

有没有什么方法可以在启动时将窗口大小调整为内容,并继续让 ListBox 随着窗口大小而不是随着其内容而增长?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瑾夏年华 2025-01-10 09:10:20

按照 Potecaru Tudor 的建议,我通过将 ListBox 包装在另一个容器中并将 ListBox 的高度绑定到容器的 ActualHeight 来解决类似的问题。

下面是一个可提供帮助的 XAML 代码片段:

<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>

基本上,Border 容器不会随着其内容的增长而增长,但它会保持拉伸到网格行的高度。网格行将填充包含窗口允许的任何空间,但不希望对窗口施加任何高度限制。因此,列表框的高度受限于边框的高度。

希望这对遇到这个令人沮丧的问题的其他人有所帮助。

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.

败给现实 2025-01-10 09:10:20

Horizo​​ntalAlignment="拉伸" VerticalAlignment="拉伸"

HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

捶死心动 2025-01-10 09:10:20

这是将 SizeToContent 属性设置为 WidthAndHeight 所导致的预期行为,即 此处描述

您可能要做的是将 ListBox 的 Width 和 Height 绑定到其容器的 ActualWidth 和 ActualHeight 属性,或者使用转换器并将它们直接绑定到 Window 的相应属性。

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.

地狱即天堂 2025-01-10 09:10:20

我希望您有以下要求,
1)ListBox应该使用滚动条,如果它的内容的大小变得比原来的大。

2) 如果调整窗口大小,ListBox 应随窗口一起增大/缩小。

我已经尝试了一个简单的示例,请检查它,如果它与您的要求不同,请告诉我,

XAML 代码:

<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# 代码:

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));
        }
    }
}

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));
        }
    }
}
长途伴 2025-01-10 09:10:20

要使 ListBox 在更改窗口大小时调整大小,只需使用:

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

要在 ListBox 中启用滚动,您可以在此处找到答案,因为 ListBox 的默认模板包含 ScrollViewer。

如何在列表框中获得垂直滚动条?< /a>

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文