-1

I need to access to SelectedIndex property of a TabControl from another thread, I tried with a Dispatcher like this:

public ListView CurrentTab
{
    get
    {
        ListView listView = null;
        Action action = () =>
        {
            int currentTab = MainWindow.AppWindow.TabControl.SelectedIndex;

            //Check wich tab is opened
            switch (currentTab)
            {
                case 0: 
                    listView = MainWindow.AppWindow.PlayingControl.Playing;
                    break;
                case 1:
                    listView = MainWindow.AppWindow.AllMatchesControl.AllMatches;
                    break;
                case 2: 
                   listView = MainWindow.AppWindow.CustomMatchesControl.CustomMatches;
                    break;
            }
        };

        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(action));
            return listView;
        }
 }

but I get

System.InvalidOperationException

Unable to access the object by the calling thread because that object is owned by another thread properties.

I'm trying to return a list through the SelectedIndex, what am I doing wrong?

Community
  • 1
  • 1
AgainMe
  • 760
  • 4
  • 13
  • 33
  • Could you tell why do you need to access selectedindex via different thread?If you access how can you syncronize them?.In other words, if you return selectedindex from another thread just before the value changed, how could you catch the value? – FreeMan Sep 21 '16 at 13:50
  • @FreeMan 'cause I need to return a specific list, each tab contains a list, so if is selected the tab 2, I need to return the list CustomMatches. And the application have different thread that call `CurrentTab` – AgainMe Sep 21 '16 at 13:51
  • You have to use the Dispatcher that the TabControl belongs to. So if you're on a different thread, and you write `Application.Current.Dispatcher`, it won't necessarily give you the dispatcher you're looking for. Every FrameworkElement has a property containing its dispatcher thread. – Meloviz Sep 21 '16 at 14:52

2 Answers2

0

As you know it will be done via another thread other than the UI, just invoke what you need

int currentTab = 0;
MainWindow.AppWindow.TabControl.Dispatcher.Invoke(() => {
  currentTab = MainWindow.AppWindow.TabControl.SelectedIndex;
});
jbmintjb
  • 161
  • 1
  • 10
0
public ListView CurrentTab
{
    get
    {
         return (ListView)this.Dispatcher.Invoke(
          new Func<ListView>(() => 
          { 
              ListView listView = null;
              int currentTab = tabControl.SelectedIndex;
                switch (currentTab)
                {
                    case 0:
                        listView = new ListView();
                        listView = list_1;
                        break;
                    case 1:
                        listView = list_2;
                        break;
                    case 2:
                        listView = list_3;
                        break;
                }
                return listView;
          }));
    }
}

this may work.

FreeMan
  • 1,417
  • 14
  • 20