2010-04-20 122 views
0

在SL4應用程序中,我需要重新設置我的TabItems(實際上是在標題中添加一個按鈕)。Silverlight TabItem模板無法正常工作

所以我拿了TabItem的控制模板here並添加了我想要的功能。

這似乎很好地工作,但有一個例外(我可以動態地添加的TabItems): 我覺得這個貼控制模板行爲有點「亂」:每一個在非選定的TabItem頭鼠標Hoovers公司,這個被選定的時間儘管點擊! (afaik這不是默認行爲:用戶用戶必須單擊一個標題才能使此tabitem成爲選定的標題)。

我試圖找到它爲什麼這樣表現,沒有運氣! 有沒有人可以啓發我的黑暗?

在此先感謝!

回答

0

事實證明,錯誤不是在控制模板中,而是在類中應用了樣式。

在細節:風格被應用於類是以下(在它,你會看到我的有關「錯誤行爲」的評論):

公共類WorkspaceViewModel:TabItem的 {

public WorkspaceViewModel() 
    { 
     DefaultStyleKey = typeof(WorkspaceViewModel); 
    } 
    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     Button closeButtonSel = base.GetTemplateChild("PART_CloseTopSelected") as Button; 
     Button closeButtonUnsel = base.GetTemplateChild("PART_CloseTopUnSelected") as Button; 
     if (closeButtonSel != null) 
      closeButtonSel.Click += new RoutedEventHandler(closeButtonSel_Click); 
     if (closeButtonUnsel != null) 
      closeButtonUnsel.Click += new RoutedEventHandler(closeButtonSel_Click); 

     //this part is causing the effect i was complaining about! 
     //and has to be removed 
     this.MouseEnter += delegate(object sender, MouseEventArgs e) 
     { 
      IsSelected = true; 
     }; 


    } 

    void closeButtonSel_Click(object sender, RoutedEventArgs e) 
    { 
     //this is the close request method used in the CloseTabItemCommand 
     OnRequestClose(); 

    } 


    #region CloseTabItemCommand 

    private RelayCommand closeTabItemCommand; 
    public ICommand CloseTabItemCommand 
    { 
     get 
     { 
      if (this.closeTabItemCommand == null) 
       this.closeTabItemCommand = new RelayCommand(p => this.OnRequestClose(), p => this.CanCloseTabItem()); 

      return this.closeTabItemCommand; 
     } 
    } 
    private bool CanCloseTabItem() 
    { 
     return true; 
    } 

    public event EventHandler RequestClose; 
    private void OnRequestClose() 
    { 
     if (RequestClose != null) 
      RequestClose(this, EventArgs.Empty); 
    } 
    #endregion 
}