Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
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?


XML
<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>


C#
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; }
        }
    }
}
Posted

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900