Reputation: 956
I have a datagrid which is in a tabcontrol item. When i doubleclick the row in the datagrid, i want the tabcontrol to change the tab.
Heres my code:
<TabItem
x:Name="tiDashboard"
Header="Dashboard"
Background="White">
<Grid>
<DataGrid
IsReadOnly="True"
x:Name="dgAnzeigeWerk"
AutoGenerateColumns="false"
Margin="0,10,0,249"
HeadersVisibility="Column"
RowHeight="25" HorizontalAlignment="Left" Width="492">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="Control.MouseDoubleClick" Handler="dgAnzeigeWerk_Row_DoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="auto" Header="Nummer" Binding="{Binding Kostenstellennummer}"/>
<DataGridTextColumn Width="auto" Header="Kostenstelle" Binding="{Binding Kostenstelle}"/>
<DataGridTextColumn Width="*" Header="Kosten" Binding="{Binding Kosten}"/>
</DataGrid.Columns>
</DataGrid>
<ComboBox x:Name="cbYearWerk" HorizontalAlignment="Left" Height="25" Margin="497,10,0,0" VerticalAlignment="Top" Width="98" VerticalContentAlignment="Center" SelectionChanged="cbYearWerk_SelectionChanged"/>
<Separator Height="15" Margin="0,-10,0,0" VerticalAlignment="Top"/>
</Grid>
</Grid>
</TabItem>
...
To change the tabitem i use this code:
private void dgAnzeigeWerk_Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
tabControl.SelectedItem = tiUebersicht;
}
But the tab won't change. I tried to do the same code with a button and it worked.
I also tried tabControl.SelectedIndex = 2
or tiUebersicht.IsSelected = true
but without success.
Any suggestions?
Upvotes: 1
Views: 1256
Reputation: 9827
Add e.Handled = true;
to your handler at the end.
private void dgAnzeigeWerk_Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
tabControl.SelectedItem = tiUebersicht;
e.Handled = true;
}
Actually selection is changing but its happening very fast and focus is returning back to the one containing DataGrid
. This can be verified using SelectionChanged
event of TabControl
.
Upvotes: 3
Reputation: 194
Try using Dispatcher inside of event.
Dispatcher.InvokeAsync(() => tiUebersicht.IsSelected = true);
Dispatcher.Invoke(() => tiUebersicht.IsSelected = true);
Dispatcher.InvokeAsync(() => tabControl.SelectedItem = tiUebersicht);
Dispatcher.Invoke(() => tabControl.SelectedItem = tiUebersicht);
Upvotes: 0