Skip to main content

Microsoft Silverlight

Answered Question Default HierarchicalDataTemplate for TreeViewRSS Feed

(0)

grimus
grimus

Member

Member

5 points

16 Posts

Default HierarchicalDataTemplate for TreeView

I'm creating a control that inherits from the Silverlight Toolkit's TreeView...
Is there a way to give the new control a default HierarchicalDataTemplate?

Currently, I have the definition of the data template in xaml right along with the the actual control. It should be unnecessary to have to explicitly define the data template everytime since the control will be binding itself to known data types.

Thanks!

JustinAngel
JustinAngel

Contributor

Contributor

4415 points

596 Posts

Answered Question

Re: Default HierarchicalDataTemplate for TreeView

Yep, that's just plain old Generic.xaml.

1. Create a directory called "tthemes".
2. Add a file named "generic.xaml" in it with the following content:

<ResourceDictionary xmlns="http://schemas.microsoft.com/client/2007"

                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

>

</ResourceDictionary>

3. Make sure the generic.xaml has a Build Action of Resource and no custom tool.
4. Create you TreeView type with a DefaultStyleKey:

    public class myTreeView : TreeView

    {

        public myTreeView()

        {

            this.DefaultStyleKey = typeof(myTreeView);

        }

    }

 5. Set the default ItemTemplate in the generic.xaml file:

<ResourceDictionary xmlns="http://schemas.microsoft.com/client/2007"

                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

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

                   xmlns:SL_40307_VS="clr-namespace:SL_40307_VS;assembly=SL_40307_VS">

    <Style TargetType="SL_40307_VS:myTreeView">

        <Setter Property="ItemTemplate">

            <Setter.Value>

                <common:HierarchicalDataTemplate ItemsSource="{Binding Items}" >

                    <TextBlock Text="{Binding myString}" />

                </common:HierarchicalDataTemplate>

            </Setter.Value>

        </Setter>

    </Style>

</ResourceDictionary>

6. Add a TreeView to your page with some data.

<SL_40307_VS:myTreeView x:Name="trv" />

            trv.ItemsSource = new ObservableCollection<myItem>()

                                  {

                                      new myItem("Hello",

                                          new myItem("World"),

                                          new myItem("Foo"),

                                          new myItem("Bar")),

                                      new myItem("Moo",

                                          new myItem("Boo",

                                            new myItem("Goo"))),

                                  };

 

    public class myItem

    {

        public myItem(string myString)

        {

            this.myString = myString;

        }

 

        public myItem(string myString, params myItem[] myItems)

            : this(myString)

        {

            ObservableCollection<myItem> itemsObservableCollection = new ObservableCollection<myItem>();

            foreach (var item in myItems)

                itemsObservableCollection.Add(item);

            Items = itemsObservableCollection;

        }

 

        public string myString { get; set; }

        public ObservableCollection<myItem> Items { get; set; }

    }

 

--
Justin Angel,
Blog @ http://silverlight.net/blogs/JustinAngel
Twitter @ http://twitter.com/JustinAngel

grimus
grimus

Member

Member

5 points

16 Posts

Re: Re: Default HierarchicalDataTemplate for TreeView

Thank you Justin!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities