如何使用C#在Silverlight 4.0中动态地将ItemTemplate添加到Treeview [英] how to add ItemTemplate to treeview dynamicaly in silverlight 4.0 using C#

查看:86
本文介绍了如何使用C#在Silverlight 4.0中动态地将ItemTemplate添加到Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


xmlns:controls_tree="clr-namespace:System.Windows;assembly=System.Windows.Controls"

<usercontrol.resources>
        <controls_tree:hierarchicaldatatemplate x:name="EmailItemTemplate" itemssource="{Binding Items}" xmlns:x="#unknown" xmlns:controls_tree="#unknown">
            <stackpanel orientation="Horizontal">
                &lt;Image  Height="15" Width="25" Source="{Binding Image}" /&gt;
                <sdk:label margin="3,0,3,0" fontsize="11" content="{Binding Title}" xmlns:sdk="#unknown" />
                <sdk:label margin="3,0,5,0" content="{Binding NodeId}" xmlns:sdk="#unknown" />
            </stackpanel>
        </controls_tree:hierarchicaldatatemplate>
    </usercontrol.resources>

<temp:mytreeview nodeexpanded="MyTreeView_NodeExpanded" scrollviewer.verticalscrollbarvisibility="Auto" height="Auto" horizontalalignment="Left" removed="AliceBlue" x:name="treeView2" style="{StaticResource ConnectingLinesTreeView}" itemtemplate="{StaticResource EmailItemTemplate}" verticalalignment="Top" width="309" borderthickness=".5" borderbrush="LightGray" xmlns:x="#unknown" xmlns:temp="#unknown" />


我已经在.xml中创建了,但是现在我想在.xaml.cs中创建以上代码


I have created in .xml but now i want create above code in .xaml.cs

MyTreeView[] mt = new MyTreeView[50];
mt[0] = new MyTreeView();


现在我有一个问题,如何通过使用上面的HierarchicalDataTemplate将ItemTemplate添加到mt对象.
请帮助我.


now i hve problem that how to add ItemTemplate to mt object by using above HierarchicalDataTemplate.
Plz help me.

推荐答案



您应该将Template-resource(直接或通过合并的ResourceDictionary)移动到Application.Resources中.然后您可以通过
实例化它
Hi,

you should move the Template-resource into the Application.Resources (either directly or via a merged ResourceDictionary). Then you can instantiate it by
DataTemplate mytemplate = Application.Current.Resources["EmailItemTemplate"] as DataTemplate;
mt[0].ItemTemplate = mytemplate;


我不确定这是否可以与资源"Name"属性一起使用;通过"Key"属性,它将并且我仅将其与样式资源一起使用,但我认为其他资源也可以使用.

干杯.


I''m not quite sure if that works with the resources "Name" property; with the "Key" property it will and I''ve only used this with Style-resources but I think other resources will work as well.

Cheers.


这篇关于如何使用C#在Silverlight 4.0中动态地将ItemTemplate添加到Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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