选项卡更改时如何执行操作

本文关键字:执行 操作 何执行 选项 | 更新日期: 2023-09-27 18:21:41

我遇到一个问题,每当单击TabItem标题时(即选择了新选项卡),我都需要从DataGrid中删除数据。问题是,使用SelectionChanged会在单击DataGrid时触发事件。

我试图找到不同的解决方案,比如在TabItem.Header中使用标签(根据SO上的另一个线程),但这导致它失去了(Metro-MaHapps)使用的风格。我尝试了MouseLeftButtonDown,但在TabItem上没有触发。

那么,还有什么其他事件我可以使用吗?

样本代码:

    <Controls:MetroAnimatedSingleRowTabControl Grid.Row="1">
        <TabItem Header="shifts">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.5*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="0.5*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.5*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <DataGrid Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="5"  Name="ShiftGridView" />
                <Button Style="{DynamicResource SquareButtonStyle}" Width="100" Height="35" Content="Start shift" Grid.Column="2" Grid.Row="2" Click="StartButton_Click" />
                <Button Style="{DynamicResource AccentedSquareButtonStyle}" Width="100" Height="35" Content="Stop shift" Grid.Column="4" Grid.Row="2"  Click="StopButton_Click" />
            </Grid>
        </TabItem>
        <TabItem Header="stats">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.5*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="0.5*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.40*" />
                    <RowDefinition Height="0.40*" />
                    <RowDefinition Height="0.40*" />
                    <RowDefinition Height="0.40*" />
                    <RowDefinition Height="3*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <TextBlock Text="Dates"
                       FontSize="14"
                       VerticalAlignment="Bottom"
                       Grid.Row="0" Grid.Column="1"/>
                <DatePicker Width="120" Height="30"
                        HorizontalAlignment="Left"
                        FontSize="14"
                        Controls:TextboxHelper.Watermark="Start date" 
                        Grid.Row="1" Grid.Column="1" Name="StartDatePicker" />
                <DatePicker Width="120" Height="30"
                        HorizontalAlignment="Left"
                        FontSize="14"
                        Controls:TextboxHelper.Watermark="Stop date" 
                        Grid.Row="2" Grid.Column="1" Name="StopDatePicker" />
                <TextBlock Text="Name"
                       FontSize="14"
                       VerticalAlignment="Bottom"
                       Grid.Row="0" Grid.Column="2"/>
                <ComboBox Width="200" Height="30"
                          HorizontalAlignment="Left"
                          SelectedIndex="0"
                          Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Name="NameComboBox"/>
                <DataGrid Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="5" Name="StatsGridView" />
                <Button Style="{DynamicResource SquareButtonStyle}" Width="100" Height="35" Content="Show stats" HorizontalAlignment="Right" Grid.Column="3" Grid.Row="5" Click="ShowStatsButton_Click" />
                <Button Style="{DynamicResource AccentedSquareButtonStyle}" Width="100" Height="35" Content="Print stats" Margin="10,0,0,0" HorizontalAlignment="Left" Grid.Column="4" Grid.Row="5" Click="PrintButton_Click" />
            </Grid>
        </TabItem>
</Controls:MetroAnimatedSingleRowTabControl>

选项卡更改时如何执行操作

SelectionChanged是Routed event,这就是它从DataGrid路由到父TabControl的原因。

要区分事件,您可以检查e.OriginalSource,它"告知"负责引发事件的实际元素。

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Sender will always be your TabControl.
    // So, just check if OriginalSource is same as sender (TabControl).
    if (e.OriginalSource == sender)
    {
        // Put your code here.
    }
}