views:

325

answers:

1

Hello everybody!

In a SL4 application i need to restyle my TabItems (actually add a button in the header).

So i took the TabItem's control template from here and added the functionality i wanted.

This seems to work fine, (i could dynamically add tabitems) with one exception: i think this posted control template is behaving somehow "arbitrary": every time the mouse hoovers over a non selected TabItem header, this gets selected WHITHOUT clicking!! (afaik this is not the default behavior: the user user has to click a header to make this tabitem the selected one).

I tried to find why it is behaving like this, with no luck! Is there someone who can enlighten my darkness???

Thanks in advance!

A: 

Well it turns out the error was not in the control template but in the class, the style was applied to.

In detail: the class the style was applied to is the following (in it you will see my comment about the "wrong behavior"):

public class 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
}
Savvas Sopiadis