奇怪的WPF ListBox滚动行为 [英] Strange WPF ListBox Scrolling Behavior

查看:60
本文介绍了奇怪的WPF ListBox滚动行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have what would appear to be a very simple issue that would have a simple solution without having to add multiple lines of code. I am working on a simple checkbook bookkeeping application. The UI has a line of comboxes (cboBroker, cboCatagory, cboSubCatagory, cboDebit, cboCredit) and a corresponding line of listboxes. When an item is selected in the combobox, it is then added to the corresponding listbox and scrolled to the bottom of the list.  Everything works fine until an identical item is repeatedly selected from the combobox and added to the listbox. For instance, suppose the Category "General Expense" (or in the case of the attached code, "Catagory4") is selected two or three times in a row. At that point the repeated item is not scrolled to the bottom. However, when a new, not identical item, is then added all the items are scrolled to the bottom. I have tried a number of ways to scroll (these are commented out in the ‘code behind" and none seem to change this listbox scrolling behavior. Attached is the code, XAML and C# "code behind, of a minimal example (one combobox and one listbox) that demonstrate the behavior.

Does anyone have a simple way to have the contents of the listbox scroll to the bottom even with repeated identical entries?







<Window x:Class="WPFSimpleScrolling.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="SimpleSolution" Height="350" Width="375">
    <Grid x:Name="grdContent" HorizontalAlignment="Left" removed="AliceBlue" Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="150"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="230"/>
        </Grid.ColumnDefinitions>
        <StackPanel x:Name="stkButtons" Width="Auto" Height="Auto" Margin="5,5,5,5" >
            <Button x:Name="btnReturn" Content="Return" HorizontalAlignment="Left" VerticalAlignment="Top" 

                        MinWidth="75" Height="35" Margin="5,10,0,5" Click="btnReturn_Click"/>
        </StackPanel>
        <WrapPanel x:Name="wrpListBoxes" Width="Auto" MaxHeight="120" Grid.Row="0" Grid.Column="1" 

                   Margin="30,10,30,10" Orientation="Horizontal" removed="AliceBlue">
            <ListBox x:Name="lstCatagory" Width="125" Height="60" 

                    Margin="5,10,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" 

                    ClipToBounds="True" ScrollViewer.HorizontalScrollBarVisibility="Hidden"

                    ScrollViewer.VerticalScrollBarVisibility="Hidden" removed="LightYellow"/>
        </WrapPanel>
        <WrapPanel x:Name="wrpComboBoxes" Width="Auto" Height="Auto" Grid.Row="1" Grid.Column="1" 

                   Margin="30,5,30,10" Orientation="Horizontal" removed="AliceBlue" >
            <ComboBox  x:Name="cboCatagory" Width="125" Height="20" Margin="5,10,0,0" 

                   HorizontalAlignment="Left" VerticalAlignment="Top" removed="PaleGreen"

                   ItemsSource="{Binding Path=EntryList}" DisplayMemberPath="Catagory" 

                   DropDownClosed="cboCatagory_DropDownClosed" />
        </WrapPanel>
    </Grid>
</Window>







using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace WPFSimpleScrolling
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string Catagory;
        int i = 0;
        string Num;
        Transactions transaction = new Transactions();
        List<Transactions> EntryList = new List<Transactions>();

        public MainWindow()
        {
            InitializeComponent();

//      Create a list of Brokers, Catagories and SubCatagories to add to comboboxes
            for (i = 1; i <= 10; i++)
            {
                Num = i.ToString();
                Catagory = "Catagory" + Num;
                EntryList.Add(new Transactions() { Catagory = Catagory });
            }
            cboCatagory.ItemsSource = EntryList;
        }

        private void btnReturn_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void cboCatagory_DropDownClosed(object sender, EventArgs e)
        {
            Catagory = cboCatagory.Text;
            lstCatagory.Items.Add(Catagory);
            lstCatagory.SelectedIndex = lstCatagory.Items.Count - 1;
            lstCatagory.ScrollIntoView(lstCatagory.SelectedItem);
            //var listBoxItems = lstCatagory.Items;
            //object lastItem = lstCatagory.Items[lstCatagory.Items.Count - 1];

            //listBoxItems.MoveCurrentTo(lastItem);
            //lstCatagory.ScrollIntoView(lastItem);

            //lstCatagory.Items.MoveCurrentToLast();
            //lstCatagory.ScrollIntoView(lstCatagory.Items.CurrentItem);

            //lstCatagory.ScrollIntoView(lstCatagory.SelectedItem);
        }

//  Create a Transaction type to add to List<Transaction>

        public class Transactions
        {
            public string Catagory { get; set; }
        }
    }
}

推荐答案

正确的方法是 ListBox.ScrollIntoView 。它确实有效。你确实使用这种方法,但可能以一种奇怪的方式,在 cboCatagory_DropDownClosed 中,我不知道为什么。在添加元素时使用它,在其他情况下你感兴趣。



-SA
The right method is ListBox.ScrollIntoView. It does work. You do use this method, but probably in a weird way, in cboCatagory_DropDownClosed, I have no idea why. Use it, say, when you add an element, in other cases you are interested in.

—SA


这篇关于奇怪的WPF ListBox滚动行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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