Jump to content

  • Log In with Google      Sign In   
  • Create Account


WPF - Where should I store the state of a scrollbar between datacontext changes?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 maya18222   Members   -  Reputation: 191

Like
0Likes
Like

Posted 17 February 2013 - 04:44 AM

If I have a datatemplate that will end up using a scrollbar in one
form or another, how can I retain the scrollbars position when the
datacontext changes back and forth?



I've read that you generally store the state of the UI in the
ViewModel. This makes sense for things like selected item, or is
expanded type states, but Im not sure how to apply it to the scrollbar
state. As in general, the scrollbar will automatically appear disappear.



Ive posted an example of what I mean below. When you select a tab and
move the scrollbar, then select another tab and move its scrollbar.
Upon returning to the first tab, the scrollbar has moved.



I believe this is due to the View being shared, and so it retains the percentage scrolled from the previous view?

 

<Window x:Class="WpfApplication13.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TabControl ItemsSource="{Binding Data}">

        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Count}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>

        <TabControl.ContentTemplate>
            <DataTemplate>
                <ListBox ItemsSource="{Binding}"/>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Window>

namespace WpfApplication13
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Data = new List<string[]>();

            for (int i = 1; i < 10; ++i)
            {
                string[] strings = new string[i * 10];
                for (int j = 0; j < i * 10; ++j)
                {
                    strings[j] = new string('z', 100);
                }

                Data.Add(strings);
            }

            DataContext = this;
        }

        public List<string[]> Data
        {
            get;
            set;
        }
    }
}

 



Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS