I am experiencing a very weird problem: In WPF I have a tabControl which contains 3 tabItems. I have made a binding from the tabControl's SelectedIndex to a property inside my viewModel class in "OneWayToSource" mode.
Here's the XAML code:
<TabControl Name="mainTabControl" SelectedIndex="{Binding SelectedTabIndex, Mode=OneWayToSource}" >
<TabItem Header="Tab 01" Name="tab01"> ... </TabItem>
<TabItem Header="Tab 02" Name="tab02"> ... </TabItem>
<TabItem Header="Tab 03" Name="tab03"> ... </TabItem>
</TabControl>
And in my viewModel:
private int m_selectedTabIndex;
public int SelectedTabIndex
{
get
{ return m_selectedTabIndex; }
set
{
SetAndNotify(ref m_selectedTabIndex, value, () => SelectedTabIndex);
SelectedTabChanged();
}
}
private void SelectedTabChanged()
{
// Some code
}
As you can see, everytime my viewModel's SelectedTabIndex
property changes, the SelectedTabChanged()
method is executed, this works perfectly.
My weird problem is that: When I show a message using for example System.Windows.MessageBox.Show("Some Text")
inside my SelectedTabChanged()
method, I select another tabItem and the previous selected tab gets blocked, it looks like selected, but it remains selected permanently, I cannot see its content anymore.
Just to clarify: As I stated before, this weird issue only happens when a modal window is showed
Why is happening? How can I solve this issue?
I hope I explained myself clearly.
Thank you in advance.
I have solved my problem. Since I am new in WPF I really dont understand why a modal window make the tabs get blocked. But I was searching and found that the Dispatcher class allows one to execute asynchronously a method which prevent any control from get blocked.
I changed my viewModel code as below:
public int SelectedTabIndex
{
get
{ return m_selectedTabIndex; }
set
{
SetAndNotify(ref m_selectedTabIndex, value, () => SelectedTabIndex);
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(SelectedTabChanged), null);
}
}
The line that really helped me was the following:
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(SelectedTabChanged), null);
Hope this help someone else can be experiencing some similar problem.