派生的TreeViewItem类不继承重写的默认模板 [英] Derived TreeViewItem class not inheriting overridden default template

查看:63
本文介绍了派生的TreeViewItem类不继承重写的默认模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在TreeViewItem中包含一段数据,这样当我从TreeView中选择它时,我可以检索与该特定Item关联的数据。这就是派生类的设置方式。



I wanted to include a piece of data with the TreeViewItem so that when I selected it from the TreeView, I could retrieve the data associated with that particular Item. This is how the derived class is set up.

class Derived_TreeViewItem : TreeViewItem
{
    public Object mObject;

    public Derived_TreeViewItem( Object obj )
    {
        mObject = obj;
    }
}





当我创建一个新的Derived_TreeViewItem时,它使用原始的TreeView模板。我替换了我的app.xaml中的原始模板,并且在非派生的TreeViewItem中完美地工作。



如何让TreeViewItem的覆盖模板工作使用派生类?



When I create a new Derived_TreeViewItem, it uses the original TreeView template. I replaced the original template in my app.xaml, and that works perfectly in a non-derived TreeViewItem.

How can I get the overriden template for the TreeViewItem to work with a derived class?

推荐答案

在XAML中:

In XAML:
<Window

    ...

    xmlns:local="clr-namespace:Some_Namespace"

    ...

    >

    <Window.Resources>
        <Style TargetType="{x:Type local:Derived_TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}" />
    </Window.Resources>

...

</Window>





在C#中:



In C#:

public void AddObjectToTree( Object obj )
{
    Derived_TreeViewItem item = new Derived_TreeViewItem( obj );
    item.Header = obj.ToString();
    Some_TreeView.Items.Add( item );
}





我发现如果Header被修改,树会自动更新Header的名称而不需要一个ObservableCollection,或者调用:





I found that if the Header is modified, the tree automatically updates the name of the Header without the need for an ObservableCollection, or calling:

Some_TreeView.Items.Refresh();
Some_TreeView.UpdateLayout();


这篇关于派生的TreeViewItem类不继承重写的默认模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆