【问题标题】:How to stop Listbox from growing on screen as new items are inserted插入新项目时如何阻止列表框在屏幕上增长
【发布时间】:2014-04-05 03:34:27
【问题描述】:

我的 WPF 应用程序有一个包含ListBox 的窗口。出于某种我不明白的原因,在将项目插入其中后,它今天开始在屏幕上生长。我希望它的高度保持固定,并且我不希望它在每次插入项目时都增加。

这是窗口的 XAML:

<Viewbox Stretch="Uniform">
    <Grid Background="{DynamicResource WindowBackground}"
          Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Background="{DynamicResource AlarmTitleBackground}"
              Grid.Row="0"
              MouseLeftButtonDown="LeftMouseButtonDown">
            . . .
        </Grid>

        <Rectangle Fill="{DynamicResource AlarmTitleBackground}"
                   Grid.Row="1"
                   Height="4" />

        <Grid Background="{DynamicResource ControlBackground}"
              Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid FocusManager.IsFocusScope="True"
                  Grid.Column="0"
                  Grid.Row="0"
                  Name="PendingAlarmScope">
                <ListBox Background="{DynamicResource TextBackground}"
                         HorizontalContentAlignment="Center"
                         IsSynchronizedWithCurrentItem="True"
                         Margin="5"
                         MinWidth="185"
                         VerticalAlignment="Stretch"
                         Name="AlarmsList"
                         SelectionChanged="AlarmsList_SelectionChanged" />
            </Grid>
            . . .
        </Grid>
    </Grid>
</Viewbox>

我在博客中发现了一篇关于 TextBox 的帖子,该帖子随着字符的输入而不断增长。作者指出TextBoxScrollViewer 中,HorizontalScrollBarVisibility 属性设置为“自动”。他们将其更改为“禁用”,这为他们修复了它。我尝试添加另一个控件的 ScrollViewer.VerticalScrollBarVisiblility="Disabled" to the xaml but this didn't work. I also tried binding theListBox 的 MaxHeight property to theActualHeight`,该控件的高度与我想要的完全相同,但也没有用。

如何在不设置的情况下修复ListBoxHeight?我希望ListBox 始终填充它所在的Grid 单元格,并且窗口可以根据不同的屏幕分辨率自行增长和重新缩放。

【问题讨论】:

标签: c# wpf xaml listbox


【解决方案1】:

经过数小时的尝试,我终于咬紧牙关,将ListBoxHeight 设置为我在使用Snoop 开始增长之前发现的值。每次插入新项目时,这都会阻止它增长。这是我能找到的唯一可行的方法。非常沮丧的一天。

【讨论】:

  • 我在 Grids 和 DockPanels 中的 ListBox 也遇到了类似的问题(没有明确的 ViewBox)。列表框的大小最初被调整为适合其他控件(如预期的那样),但即使在列表框上设置了ScrollViewer.VerticalScrollBarVisibility="Visible"VerticalAlignment="Stretch" 时,添加项目(不希望的)也会增长:(我们也最终感到沮丧并且有硬编码高度。
【解决方案2】:
<Grid Background="{DynamicResource ControlBackground}"
              Grid.Row="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

为了笑容,你会尝试放下视图框和其他一些东西

<Grid Background="{DynamicResource WindowBackground}"
          Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Rectangle Fill="{DynamicResource AlarmTitleBackground}"
                   Grid.Row="1"
                   Height="4" />


                <ListBox Grid.Row="2" Background="{DynamicResource TextBackground}"
                         HorizontalContentAlignment="Center"
                         IsSynchronizedWithCurrentItem="True"
                         Margin="5"
                         MinWidth="185"
                         VerticalAlignment="Stretch"
                         Name="AlarmsList"
                         SelectionChanged="AlarmsList_SelectionChanged" />

    </Grid>

【讨论】:

  • 谢谢,但这没有帮助。行为没有变化。
  • 我只删除了ViewBoxListBox 停止增长。但是,需要ViewBox 才能使控件在不同的分辨率下看起来正确。我理解为什么ListBoxViewBox 中时会增长——ViewBox 使它认为它有无限的空间。我需要在不增加 ListBox 的情况下对布局产生影响。
  • 所以问题出在 ViewBox 上,甚至不值得 +1。您在 Grid 中尝试过 ViewBox(es) 吗?
  • 老兄,没必要剪短。我给了你+1。我仍在努力解决这个问题,如果我没有为你做得足够快,我很抱歉。
猜你喜欢
  • 2015-08-04
  • 1970-01-01
  • 2023-03-09
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2017-05-08
  • 1970-01-01
相关资源
最近更新 更多