简体   繁体   English

WPF,C#-双击数据网格的行来更改选定的选项卡

[英]WPF, C# - change selected tab by doubleclick on a datagrid's row

I have a datagrid which is in a tabcontrol item. 我在tabcontrol项中有一个数据网格。 When i doubleclick the row in the datagrid, i want the tabcontrol to change the tab. 当我双击数据网格中的行时,我希望tabcontrol更改选项卡。

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. 我也尝试了tabControl.SelectedIndex = 2tiUebersicht.IsSelected = true但没有成功。

Any suggestions? 有什么建议么?

Add e.Handled = true; 添加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 . 实际上选择正在改变,但是它发生得非常快,并且焦点又回到了包含DataGrid This can be verified using SelectionChanged event of TabControl . 可以使用TabControl SelectionChanged事件对此进行验证。

Try using Dispatcher inside of event. 尝试在事件内部使用Dispatcher

Link to similar question 链接到类似问题

Dispatcher.InvokeAsync(() => tiUebersicht.IsSelected = true);
Dispatcher.Invoke(() => tiUebersicht.IsSelected = true);
Dispatcher.InvokeAsync(() => tabControl.SelectedItem = tiUebersicht);
Dispatcher.Invoke(() => tabControl.SelectedItem = tiUebersicht);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM