标签更改时如何执行操作?

时间:2014-09-13 13:15:42

标签: c# wpf datagrid

我遇到一个问题,即只要点击DataGrid标题(即已选择新标签页),我就需要从TabItem删除数据。问题是,使用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>

1 个答案:

答案 0 :(得分:0)

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.
    }
}