WPF列表框不会滚动垂直 [英] WPF Listbox wont scroll Vertical

查看:97
本文介绍了WPF列表框不会滚动垂直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Groupbox中,我有一个Listbox,Xbox中也定义了ListboxItems.列表框已定义:

Within a Groupbox I have a Listbox, ListboxItems are defined in the XAML as well. The Listbox is defined:

<ListBox Name="lvAvoidCountry" Margin="5,5,5,5"  
    Background="Transparent" 
    ScrollViewer.VerticalScrollBarVisibility="Visible"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" >

项目的定义如下:

<ListViewItem >
  <CheckBox Name="chkAlbanien" Tag="55">
    <StackPanel Orientation="Horizontal">
      <Image Source="images/flag_albania.png" Height="30"></Image>
      <TextBlock Text="Albanien" Margin="5,0,0,0"></TextBlock>
    </StackPanel>
  </CheckBox>
</ListViewItem>

如果我删除了Scrollviewer设置,则会进行水平滚动,并且各项的格式正确-宽度正确.如果我使用scrollviewer设置,则项目将被剪切掉,以便将所有项目放置在列表框中. (例如,显示了标志,显示了复选框,但文本仅为"Alba").

If I remove the Scrollviewer Settings I get horizontal scrolling and the Items are well formatted - correct width. If I use the scrollviewer settings the items get cut off so that all items are placed on the listbox. (eg. the flag is shown, the checkbox is shown but the text is just "Alba").

感谢任何提示!

推荐答案

顾名思义,ScrollViewer.HorizontalScrollBarVisibility="Disabled"禁用水平滚动.如果这样做,但是ListBoxItems太长,它们将被切断. StackPanel不会增长或缩小以适合ListBox,即使它太窄,它也不会包装"您的项目以适合ListBox,即使您将TextWrapping添加到TextBlock中也是如此.非常固执.我认为您的主要问题是StackPanel.

As the name implies, ScrollViewer.HorizontalScrollBarVisibility="Disabled" disables horizontal scrolling. If you do that, but your ListBoxItems are too long, they'll get cut off. The StackPanel won't grow or shrink to fit into the ListBox, and it won't "wrap" your items to fit into the ListBox if it's too narrow, even if you add TextWrapping to the TextBlock. It's very stubborn. I think your main problem is that StackPanel.

而不是StackPanel,请尝试使用具有2个列的Grid,如下所示:

Instead of a StackPanel, try using a Grid with 2 columns defined like so:

<ListViewItem >
  <CheckBox Name="chkAlbanien" Tag="55">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image Grid.Column="0" Source="images/flag_albania.png" Height="30"/>
        <TextBlock Grid.Column="1"
                   TextWrapping="Wrap"
                   Text="Albanien" Margin="5,0,0,0"/>
    </Grid>
  </CheckBox>
</ListViewItem>

Auto将收缩"图像列,而*将为文本保留所有剩余空间.然后在您的文本块中添加TextWrapping,以防它仍然太长.

Auto will "shrinkwrap" the image columns, and * will give the text all remaining space. Then add TextWrapping to your textblock in case it's still too long.

添加了更完整的代码示例,并略微更改了我的答案.

Edited: added more complete code example and changed my answer slightly.

这篇关于WPF列表框不会滚动垂直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆