I was playing with TabControl, and I tried to implement SelectionChange event of TabControl.
So I did something like this:
XAML:
<Window x:Class="WpfApplication20.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TabControl SelectionChanged="TabControl_SelectionChanged"> <TabItem Header="Test1"></TabItem> <TabItem Header="Test2"></TabItem> </TabControl> </Grid> </Window>
Code behind:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { MessageBox.Show("test"); }
With that code I was receiving error:
Dispatcher processing has been suspended, but messages are still being processed.
Solution:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(this.MyTest)); } private void MyTest() { MessageBox.Show("test"); }
It seems that problem is in multi thread... Because for example code like this:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { // ... Get TabControl reference. var item = sender as TabControl; // ... Set Title to selected tab header. var selected = item.SelectedItem as TabItem; this.Title = selected.Header.ToString(); }
Works without problem.