2016-07-14 27 views
1

我有一个TabControl与多个TabItems的ItemsSource和一个额外的TabItem,添加新的TabItems到这个列表。负责添加项目的TabItem监听MouseUp事件。 问题是,添加项目后,我更新SelectedIndex,但是然后SelectedIndex更改为我单击的元素。在XAML的WPF - 如何停止TabControl在MouseUp事件中更改索引?

部分:

<TabControl ItemsSource="{Binding Tabs}" SelectedIndex="{Binding SelectedIndex}"> 

我的代码:

public ObservableCollection<TabItem> Tabs { get; set; } 
public int SelectedIndex { get; set; } 

private void Tab_add_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    e.Handled = true; 
    int count = Tabs.Count; 
    TabItem tabItem = new TabItemDepartment("Header #x"); 
    Tabs.Insert(count - 1, tabItem); 
    SelectedIndex = count - 2; 
} 

编辑:最终的代码(仅适用于相关的部分)的基础上的答案:

public class CalendarViewModel : INotifyPropertyChanged 
{ 
    public ObservableCollection<TabItem> Tabs { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private int _SelectedIndex; 
    public int SelectedIndex 
    { 
     get 
     { 
      return _SelectedIndex; 
     } 
     set 
     { 
      _SelectedIndex = value; 
      NotifyPropertyChanged(nameof(SelectedIndex)); 
     } 
    } 

    private void Tab_add_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     TabItem tabItem = new TabItemDepartment("Header #x"); 
     Tabs.Insert(Tabs.Count - 1, tabItem); 
     SelectedIndex = Tabs.Count - 2; 
     e.Handled = true; 
    } 

    protected void NotifyPropertyChanged(string propertyName = null) 
    { 
     this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

尝试在预览mousedown事件做,如果'e.Handled'从路由时停止的情况下,那么它应该工作。 – Jai

+0

工作,但它不会改变SelectedIndex。所以当我点击添加选项卡时,索引保持不变。 – Johannes

回答

2

您需要使用或者DependencyPropertyINotifyPropertyChanged,因为绑定引擎不知道的变化财产。

public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register(
    "SelectedIndex", 
    typeof(int), 
    typeof(WhateverYourClassNameIs)); 

public int SelectedIndex 
{ 
    get { return (int)GetValue(SelectedIndexProperty); } 
    set { SetValue(SelectedIndexProperty, value); } 
} 

或者,您也可以让这个类实现INotifyPropertyChanged,但一般来说,已经从FrameworkElement(类与UI)派生的类,DependencyProperty将是首选。

+0

所以我必须将我的ViewModel更改为'公共类CalendarViewModel:DependencyObject'(这是事件处理程序定义和注册的地方),并将'typeof'更改为'typeof(CalendarViewModel)'?我试过这个,但它仍然不会更新索引。 – Johannes

+0

如果您已经在使用MVVM,请使用INotifyPropertyChanged。网上有很多例子。 @Johannes – Jai

1

你有2个问题在这里:

  1. SelectedIndex确实需要或者是DependencyProperty或您的类定义SelectedIndex需要实现INotifyPropertyChanged

尝试这种情况:

public int SelectedIndex 
{ 
    get { return (int)GetValue(SelectedIndexProperty); } 
    set { SetValue(SelectedIndexProperty, value); } 
} 

public static readonly DependencyProperty SelectedIndexProperty = 
    DependencyProperty.Register("SelectedIndex", typeof(int), typeof(YOURCLASS), new PropertyMetadata(0)); 
  • 你在鼠标向上事件逻辑是错误的。我假设你只想添加一个标签,如果最后一个项目被点击?此外,您不能使用本地计数变量,因为添加新选项卡后,集合中的项目数会发生变化。
  • 试试这个:

    private void Tab_add_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
        e.Handled = true; 
        // Only add if the user clicks on the last items. 
        if (this.SelectedIndex == this.Tabs.Count - 1) 
        { 
         TabItem tabItem = new TabItemDepartment("Header #x"); 
         this.Tabs.Insert(this.Tabs.Count - 1, tabItem); 
         // The Count has changed at this point so make sure we check the collection again. 
         this.SelectedIndex = this.Tabs.Count - 2; 
        } 
    } 
    
    +0

    没错,我正在修改代码,忘记更改Count的代码 - 因为它并不工作,我忽略了这个错误。 我尝试使用DependencyProperty,但它没有工作,所以我选择了'INotifyPropertyChanged'这确实工作。 – Johannes

    相关问题