问题:WPF TabControl切换TabItem时没有SelectionChanging事件
解决方案:可以用TabItem的PreviewMouseLeftButtonDown事件代替
XAML中:
<TabControl x:Name="tabControl1" Grid.Row="1" Grid.RowSpan="2" Grid.Column="2"
>
<TabItem x:Name="tabPage1" Header="星期一" IsSelected="True"
PreviewMouseLeftButtonDown="tabControl1_SelectionChanging">
<local:ucSchema x:Name="ucSchema1"></local:ucSchema>
</TabItem>
<TabItem x:Name="tabPage2" Header="星期二" PreviewMouseLeftButtonDown="tabControl1_SelectionChanging">
<local:ucSchema x:Name="ucSchema2"></local:ucSchema>
</TabItem>
<TabItem x:Name="tabPage3" Header="星期三" PreviewMouseLeftButtonDown="tabControl1_SelectionChanging">
<local:ucSchema x:Name="ucSchema3"></local:ucSchema>
</TabItem>
</TabControl >
.cs中:
private void tabControl1_SelectionChanging(object sender, MouseButtonEventArgs e)
{
int Index = this.tabControl1.SelectedIndex;
if (controls[Index].IsChange())
{
if (MessageBox.Show(Suno.BaseMent.Management.Language.Msg("数据已经修改,是否保存变动"), "提示", MessageBoxButton.YesNo, MessageBoxImage.Question
) == MessageBoxResult.Yes)
{
if (controls[Index].Save() == -1)
{
return;
}
}
}
}