Silverlight TabItem 模板无法正常工作

发布于 2024-08-29 16:42:41 字数 404 浏览 9 评论 0原文

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

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

这似乎工作正常,(我可以动态添加选项卡项),但有一个例外: 我认为这个发布的控件模板的行为在某种程度上是“任意的”:每次鼠标悬停在未选择的 TabItem 标题上时,都会在不单击的情况下选择它! (据我所知,这不是默认行为:用户必须单击标题才能使该选项项成为选定选项)。

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

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2024-09-05 16:42:41

事实证明,错误不在控件模板中,而是在应用样式的类中。

详细信息:应用样式的类如下(在其中您将看到我关于“错误行为”的评论):

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文