Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
XML
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" />

I have created in .xml but now i want create above code in .xaml.cs
C#
MyTreeView[] mt = new MyTreeView[50];
mt[0] = new MyTreeView();

now i hve problem that how to add ItemTemplate to mt object by using above HierarchicalDataTemplate.
Plz help me.
Posted
Updated 27-Jul-11 20:49pm
v4

1 solution

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;

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900