Wpf 用户更改选项卡项之前的事件

Wpf 用户更改选项卡项之前的事件,wpf,events,Wpf,Events,我有一个WPF应用程序,其中有一个TabControl和几个TabItems,每个TabItems包含一个UserControl。用户可以更改选项卡项中包含的usercontrol中的条目,例如配置应用程序 <TabControl> <TabItem Header="Configuration"> <views:ConfigurationView x:Name="ConfigurationView_Object" /> </TabItem

我有一个WPF应用程序,其中有一个TabControl和几个TabItems,每个TabItems包含一个UserControl。用户可以更改选项卡项中包含的usercontrol中的条目,例如配置应用程序

<TabControl>
  <TabItem Header="Configuration">
    <views:ConfigurationView x:Name="ConfigurationView_Object" />
  </TabItem>
  <TabItem Header="Artist">
    ...
  </TabItem>
</TabControl>
<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>

...
我有一个函数,用于检查UserControl中是否存在未保存的更改在用户更改选项卡或关闭应用程序之前,我想让他选择保存、放弃或保留选项卡

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>
这可能吗?如果可能,如何实现?如果它需要TabControl以外的其他控件/结构,那也可以,因为我目前正处于计划阶段

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>
提前感谢,

Frank

您可以对任何事件使用交互性。这是MVVM解决方案

<TabControl>
   <TabItem Header="Configuration">
   <views:ConfigurationView x:Name="ConfigurationView_Object" />
    <intr:Interaction.Triggers>
         <intr:EventTrigger EventName="MouseUp">
               <intr:InvokeCommandAction Command="{Binding Yourcommand}" CommandParameter="YourCommandParameter"/>
               </intr:EventTrigger>
         </intr:Interaction.Triggers>
<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>


如果要通过事件执行,可以对选项卡控件使用SelectionChanged事件,对窗口使用Closing事件。大概是这样的:

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>
XAML:

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>
XAML:

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>

TabControl没有TabChanged事件。但是,您可以使用
.Items.CurrentChanging
事件。只有在选项卡控件上设置了IsSynchronizedWithCurrentItem=“True”时,此选项才有效

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>
XAML**

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height='1*' />
      <RowDefinition Height='Auto' />
    </Grid.RowDefinitions>
    <TabControl x:Name='MainTab'
                IsSynchronizedWithCurrentItem='True'
                Grid.Row='0'>
      <TabItem x:Name='TabTickets'
               Header='Tickets'>
        
          <StackPanel Orientation='Horizontal' >
            <TextBlock Text='Provide some text:'
                       Margin='10,0' />
            <TextBox x:Name='ExampleTextBox'
                     VerticalAlignment='Top' MinWidth='90' />
          </StackPanel>
       
      </TabItem>
      <TabItem x:Name='TabCalendar'
               Header='Calendar' />
      <TabItem x:Name='TabAbout'
               Header='About' />
    </TabControl>

    <TextBlock x:Name='MessageTextBox'
               Grid.Row='1' />

  </Grid>
<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>
屏幕截图

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>
当数据不完整时,防止移动到另一个选项卡

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>

数据完成后,允许移动到另一个选项卡。

我终于自己实现了一个简单的tabcontrol,因为这样我就可以控制一切

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>

<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
  <Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
    <TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
    <TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
  </Border>
  <Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
    <TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
  </Border>
</WrapPanel>

<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
  <ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>

xmlns:intr=“clr命名空间:System.Windows.Interactivity;assembly=System.Windows.Interactivity”有趣的可能性。我不确定mouseup事件是否足以涵盖所有可能的更改选项卡的方法,然后我仍然需要阻止选项卡更改…感谢您的回复!关键是,我还希望用户能够取消更改,保持在所选选项卡上。从我在Stackoverflow上读到的()来看,这似乎相当复杂…谢谢你的回复!问题是,只要在更改期间没有用户交互,此解决方案就可以工作。(请参阅)但我想问用户,他想做什么(保存/dicard/stay)您是否尝试了该代码?用户无法与UI的任何其他部分交互,它们将被发送回原始选项卡。如果这还不够,可以使用消息框提示用户,还可以在对话框上使用按钮从用户处获取信息。这是一种模式,在消息框关闭之前,用户将无法与应用程序交互。如何向用户显示对话框并获得结果与您提出的问题不同。看见