VB.NET WPF列表框按向上/向下按钮滚动 [英] VB.NET WPF Listbox scroll by UP/DOWN button

查看:106
本文介绍了VB.NET WPF列表框按向上/向下按钮滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将表单应用翻译成wpf应用,我不得不承认这是一个真正的痛苦。直到我差点放弃。



我的许多问题之一是基本问题:



我隐藏了列表框的滚动条,并在列表框旁边有一个UP按钮和一个DOWN按钮。



使用.NET FORMS,滚动只需将列表框的topindex设置为所需位置即可轻松点击按钮。



如何使用WPF实现相同的行为?

I am trying to translate a forms app, to a wpf app, and I have to admit it is a real pain. Up to the point that I'm almost giving up.

One of my many problems is a basic one:

I hide the scrollbars for my listboxes and have an UP-Button and a DOWN-button next to the listbox.

With .NET FORMS, scrolling by clicking the button was easy by just setting the topindex of a listbox to the desired position.

How do I accomplish this same behaviour with WPF?

推荐答案

我不熟悉VB,所以我在C#中有一个解决方案。

在VB中翻译应该不难。



在示例中,一些项目被添加到列表框中。

当ListBox被加载时,会测量一个listboxItem。

然后使用该listboxItem的高度向上和向下滚动。



更好的解决方案是将一个加载的事件添加到其中一个listboxItem并获取Item的actualHeight 。

如果你的物品不是相同的高度,测量每个ListBoxItem并将这些值放在列表中。根据该列表中的偏移进行滚动。



还可以测量listboxItem相对于ListBox的位置。

Point relativePoint = ListBoxItem.TransformToAncestor(ListBoxTest).Transform(new Point(0,0));





I'm not familiar with VB, so I have a solution in C#.
Shouldn't be hard to translate in VB.

In the example some items are added to the listbox.
When the ListBox is Loaded, one listboxItem is measured.
The height of that listboxItem is then used to scroll up and down.

A better solution is to add a loaded event to one of the listboxItems and take the actualHeight of the Item.
If your Items are not the same height, measure every ListBoxItem and put those values in a list. Scroll according to the offsets in that list.

It's also possible to measure the position of the listboxItem relative to the ListBox.
Point relativePoint = ListBoxItem.TransformToAncestor(ListBoxTest) .Transform(new Point(0, 0));


<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.ColumnDefinitions>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Vertical">
            <Button MinWidth="100" Padding="5" Margin="5" Name="ButtonUp" Click="ButtonUp_Click">Up</Button>
            <Button MinWidth="100" Padding="5" Margin="5,0,5,5" Name="ButtonDown" Click="ButtonDown_Click">Down</Button>
        </StackPanel>
        <ScrollViewer Name="ScrollViewerTest" Grid.Column="1" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden">
            <ListBox Name="ListBoxTest" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Loaded="ListBoxTest_Loaded"/>
        </ScrollViewer>
    </Grid>
</Window>










public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PopulateListBox();
        }

        private void PopulateListBox()
        {
            ListBoxTest.Items.Clear();
            for (int i=0;i<100;i++)
                ListBoxTest.Items.Add(new ListBoxItem() { Content = "Item " + i.ToString() });
        }

        double ItemOffset=0;
        private void ButtonUp_Click(object sender, RoutedEventArgs e)
        {
            ScrollViewerTest.ScrollToVerticalOffset(ScrollViewerTest.VerticalOffset - ItemOffset);
        }
        
        private void ButtonDown_Click(object sender, RoutedEventArgs e)
        {
            ScrollViewerTest.ScrollToVerticalOffset(ScrollViewerTest.VerticalOffset + ItemOffset);
        }

        private void ListBoxTest_Loaded(object sender, RoutedEventArgs e)
        {
            if (ListBoxTest.Items.Count == 0) return;

            if (ItemOffset == 0)
            {
                ((ListBoxItem)ListBoxTest.Items[0]).Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                ItemOffset = ((ListBoxItem)ListBoxTest.Items[0]).DesiredSize.Height;
            }
        }
    }


这篇关于VB.NET WPF列表框按向上/向下按钮滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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