0

在 SL4 应用程序中,我需要重新设置 TabItems 的样式(实际上是在标题中添加一个按钮)。

所以我从这里获取了 TabItem 的控件模板并添加了我想要的功能。

这似乎工作正常,(我可以动态添加 tabitems)有一个例外:我认为这个发布的控件模板的行为以某种方式“任意”:每次鼠标悬停在未选择的 TabItem 标题上时,这会被选中而不点击!(afaik 这不是默认行为:用户用户必须单击标题才能使此 tabitem 成为选定的)。

我试图找出它为什么会这样,但没有运气!有没有人可以点亮我的黑暗???

提前致谢!

4

1 回答 1

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
}
于 2010-04-21T05:42:16.757 回答