简体   繁体   English

ListBox裁剪顶部或底部

[英]ListBox crop top or bottom

ListBox seems to always crop the the last item. ListBox似乎总是裁剪最后一个项目。
For example use the arrow to move up or down or the scrollbar. 例如,使用箭头向上或向下移动或滚动条。

Is it possible to crop the first item if moving down and 如果向下移动,是否可以裁剪第一个项目
and crop the last item if moving up? 如果向上移动并裁剪最后一个项目?

Only supposed to be one question at a time so this is optional. 一次只应该是一个问题,因此这是可选的。
If mouse click on the last item then crop the top? 如果鼠标单击最后一项,则裁剪顶部?
Mouse click on any other position then crop bottom. 鼠标单击任何其他位置,然后裁剪底部。

ScrollViewer.CanContentScroll="False" fixes a lot. ScrollViewer.CanContentScroll =“ False”修复了很多。
Now click on item behaves as I want. 现在,单击项目行为就像我想要的。
Up down arrow on items behaves like I want. 项目上的向下箭头的行为就像我想要的。
But scrollbar movement crops on both top bottom. 但是滚动条的运动同时出现在顶部和底部。

<Window x:Class="ListBoxLastIntoView.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>
        <ListBox x:Name="lb" Width="420" HorizontalAlignment="Left"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border Width="390" BorderBrush="Purple" Padding="2" Margin="2" BorderThickness="2">
                        <TextBlock TextWrapping="Wrap" Width="380" TextTrimming="CharacterEllipsis" Text="{Binding Mode=OneWay}"/>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>
namespace ListBoxLastIntoView
{
    public partial class MainWindow : Window
    {
        private string lorum = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
        private List<string> lorums = new List<string>();
        public MainWindow()
        {
            for (int i = 1; i < 100; i++) lorums.Add(i.ToString() + " " + lorum);           
            InitializeComponent();
            lb.ItemsSource = lorums;
        }
    }
}

If you want to remove the blank space at the end of ListBox , you can use this construction: 如果要删除ListBox末尾的空白,可以使用以下构造:

<ListBox x:Name="lb"     
         ScrollViewer.CanContentScroll="False" ... />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM