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?
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
.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.