0

我正在从 MVVM 方式开发一个 WPF 项目。

我将 Observable Collection 绑定到 XamTabControl。如果我向 Observable Collection 添加一个新项目,则会生成一个新选项卡。但是如果我关闭选项卡,选项卡项目不会从 Observable 集合中删除。

如果我可以触发选项卡的关闭事件(或关闭事件),我可以手动执行此操作。但是这两个事件没有被触发。但是某些事件会被触发,例如 MouseUp。

<igWindows:XamTabControl 
 Height="198" 
 HorizontalAlignment="Left" 
 Margin="0,54,0,0" 
 ItemsSource="{Binding Tabs}"
 SelectedItem="{Binding SelectedTab}"
 Name="xamTabControl1" 
 VerticalAlignment="Top" 
 Width="651">

     <i:Interaction.Triggers>
           <i:EventTrigger EventName="Closing">
                <i:InvokeCommandAction Command="{Binding TabCloseCommand}" />
           </i:EventTrigger>
      </i:Interaction.Triggers>

     <igWindows:XamTabControl.ItemContainerStyle>
           <Style TargetType="igWindows:TabItemEx">
                <Setter Property="Header" Value="{Binding Header}"/>
                <Setter Property="CloseButtonVisibility" Value="{Binding CloseButtonVisibility}"/>
           </Style>
      </igWindows:XamTabControl.ItemContainerStyle>

     <igWindows:XamTabControl.ContentTemplate>
          <!-- this is the body of the TabItem template-->
          <DataTemplate>
                <TextBlock Text="{Binding Content}" />
          </DataTemplate>
      </igWindows:XamTabControl.ContentTemplate>

 </igWindows:XamTabControl>

这是我的视图模型

private ObservableCollection<TabItem> tabs;
private TabItem selectedTab;
private ICommand tabCloseCommand;

public ObservableCollection<TabItem> Tabs
{
     get
     {
         return tabs;
     }
     set
     {
          tabs = value;
          NotifyPropertyChanged("Tabs");
     }
}



public TabItem SelectedTab
{
     get
     {
          return selectedTab;
     }
     set
     {
          selectedTab = value;
          NotifyPropertyChanged("SelectedTab");
     }
}



public ICommand TabCloseCommand
{
     get
     {
          if (tabCloseCommand == null)
          {
          tabCloseCommand = new RelayCommand(param => this.CloseTab(), null);
          }
          return tabCloseCommand;
      }
}



private void CloseTab()
{

}
4

2 回答 2

0

这可能是因为DataContext运行 Closing 事件的对象是您的TabItem类,而不是包含TabCloseCommand

使用ElementNameorRelativeSourceSource您的命令绑定设置为TabControl.DataContext第一个,它应该可以工作

<i:InvokeCommandAction Command="{Binding ElementName=xamTabControl1, 
                                         Path=DataContext.TabCloseCommand}" />
于 2013-02-22T14:12:11.810 回答
0

您必须连接关闭事件的代码不起作用,因为 XamTabControl 上没有 Closing 或 Closed 事件,这是关联的对象。我不确定是否可以使用 EventTrigger 从 xamTabControl 附加到 TabItemEx 的事件。如果要在没有 EventTrigger 的情况下将处理程序添加到 XamTabControl,则应执行以下操作:

<igWindows:XamTabControl 
    Name="xamTabControl1" 
    AllowTabClosing="True" 
    igWindows:TabItemEx.Closed="OnTabClosed">

</igWindows:XamTabControl>

您还可以在帮助中的“删除关闭的选项卡”主题中查看详细示例。

于 2013-02-27T20:55:32.500 回答