2016-07-14 25 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

    相關問題