Silverlight中TreeView,TreeViewItem,HierarchalDataTemplate的详细用法

Silverlight中TreeView,TreeViewItem,HierarchalDataTemplate的详细用法,第1张

概述转载自:http://blogs.silverlight.net/blogs/justinangel/archive/2008/11/18/silverlight-toolkit-treeview-treeviewitem-amp-hierarchaldatatemplate.aspx Hi folks,   I’d like to go over the TreeView control whi

转载自:http://blogs.silverlight.net/blogs/justinangel/archive/2008/11/18/silverlight-toolkit-treevIEw-treevIEwitem-amp-hIErarchaldatatemplate.aspx



Hi folks,

 

I’d like to go over the TreeVIEw control which is part of the new Silverlight Toolkit (http://www.codeplex.com/Silverlight).
We’ll talk about the TreeVIEw control itself,how to style the TreeVIEwItem and what is HIErarchalDataTemplate all about.  

Recommended reading before reading this article

1. Silverlight Toolkit: HeaderedContentControl & HeaderedItemsControl

 

 

Setup

1. Create a new project.

 

2. Add a reference to the Silverlight Controls assembly (Microsoft.windows.Controls.dll) which can be downloaded athttp://codeplex.com/Silverlight.

  

 

3. Look under "Custom Controls" In the Blend Asset library.

 

4. Add a TreeVIEw to the Page.

 

And here's the XAML Blend generated for us:

<UserControl    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/Expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    x:Class="SilverlightControlsNovember2008.TreeVIEwPage"    d:DesignWIDth="640" d:DesignHeight="480"    xmlns:controls="clr-namespace:Microsoft.windows.Controls;assembly=Microsoft.windows.Controls"    xmlns:vsm="clr-namespace:System.windows;assembly=System.windows">    <GrID x:name="LayoutRoot" Background="#FFFFFFFF">        <controls:TreeVIEw margin="79,98,0" VerticalAlignment="top" Height="200" WIDth="120" HorizontalAlignment="left"/>    </GrID></UserControl>

Manually Adding New Textual TreeVIEwItems to a TreeVIEw

Let’s add some TreeVIEwItems to our TreeVIEw that will reflect a family’s genealogy tree.  like so:

 

We’ll right click on TreeVIEw –> Add TreeVIEwItem.

And we can see that we indeed got a new TreeVIEwItem nested under or TreeVIEw:

 

Next,we’ll go to the TreeVIEwItem’s propertIEs and set header to “Sally” andIsExpanded to true.

 

 

Here’s the XAML blend generated for us up until Now:

<controls:TreeVIEw  Height="200" WIDth="120">    <controls:TreeVIEwItem IsExpanded="True" header="Sally"/></controls:TreeVIEw>

Now we’d like to add “John” as a child TreeVIEwItem of “Sally”.

We’ll Right click on “Sally” TreeVIEwItem –> Add TreeVIEwItem.

And we got another nested note insIDe the prevIoUs TreeVIEwItem:

 

We’ll edit it’s header to “John” and we’ll keep IsExpanded to false,because it has no nested nodes.

 

We’ll keep it up until we get the following TreeVIEw:

 

Let’s run our sample:

And we can start collapsing,expanding & Selecting TreeVIEwItems:

(In the above sample,Greg is collapsed,and John is selected)

 

And here’s the XAML Blend generated for us:

<controls:TreeVIEw Height="200" WIDth="120">    <controls:TreeVIEwItem IsExpanded="True" header="Sally">        <controls:TreeVIEwItem header="John"/>        <controls:TreeVIEwItem header="Greg" IsExpanded="True">            <controls:TreeVIEwItem header="linda"/>            <controls:TreeVIEwItem header="Darren"/>        </controls:TreeVIEwItem>        <controls:TreeVIEwItem header="Allice" IsExpanded="True">            <controls:TreeVIEwItem header="Neil"/>        </controls:TreeVIEwItem>    </controls:TreeVIEwItem></controls:TreeVIEw>

Editing the TreeVIEwItem’s header property in Visual Studio XAML Editor

Now it’s time to uncover the horrible truth – Sally and her children are all alIEns. The green “blip blip” kind.

This is how our TreeVIEw should look like when we’re done:

 

 

Blend currently does not support editing the header property in a visual way. So we’ll open up Visual Studio’s XAML editor for that.

Right click on “TreeVIEwPage.xaml” (which is our page) in the Blend project tab –> Edit In Visual Studio.

 

And in a few second we’ll see this screen:

 

We’ll start off by changing the first TreeVIEwItem:

<controls:TreeVIEwItem IsExpanded="True" header="Sally">

First,we’ll expand the header property to allow XAML content:
<controls:TreeVIEwItem IsExpanded="True">    <controls:TreeVIEwItem.header></controls:TreeVIEwItem.header>

We’ll fill in the “Sally” content:
<controls:TreeVIEwItem IsExpanded="True">    <controls:TreeVIEwItem.header>        <TextBlock Text="Sally" />    </controls:TreeVIEwItem.header>

nd our TreeVIEw still looks exactly the same:

 

Now we’d like to add the image of our AlIEn,but the header can only contain a single element.
So we’ll group the <Image> and the <TextBlock> in a horizontal <StackPanel>.

<controls:TreeVIEwItem.header>

    <StackPanel OrIEntation="Horizontal">

        <TextBlock Text="Sally" />

    </StackPanel>

</controls:TreeVIEwItem.header>

Now that we have a container in the header,we’ll add the <Image> tag.

<controls:TreeVIEwItem.header>

    <StackPanel OrIEntation="Horizontal">

        <Image Source="AlIEn1.png" />

        <TextBlock Text="Sally" />

    </StackPanel>

</controls:TreeVIEwItem.header>

In our Visual Studio prevIEw windows we can see the following image:

Well,The text is too small for our picture. So let’s change the FontSize for theTreeVIEw to 22.

<controls:TreeVIEwFontSize="22" Height="200" WIDth="120">

 

We can see the Horizontal and Vertical Scrollbars appear because Now our TreeVIEwItems are bigger than the TreeVIEw. We’ll change the TreeVIEwWIDth & Height accordingly. 

<controls:TreeVIEw FontSize="22" Height="350" WIDth="250">

 

Now our TreeVIEw looks much better:

 

here’s our XAML up until Now:

In our Visual Studio prevIEw windows we can see the following image:

Well,The text is too small for our picture. So let’s change the FontSize for the TreeVIEw to 22.

<controls:TreeVIEw FontSize="22" Height="200" WIDth="120">

 

We can see the Horizontal and Vertical Scrollbars appear because Now our TreeVIEwItems are bigger than the TreeVIEw. We’ll change the TreeVIEw WIDth & Height accordingly. 

<controls:TreeVIEw FontSize="22" Height="350" WIDth="250">

 

Now our TreeVIEw looks much better:

 

here’s our XAML up until Now:

<controls:TreeVIEw FontSize="22" Height="350" WIDth="250" margin="84,101,0" VerticalAlignment="top" HorizontalAlignment="left">    <controls:TreeVIEwItem IsExpanded="True">       <controls:TreeVIEwItem.header>            <StackPanel OrIEntation="Horizontal">                <Image Source="AlIEn1.png" />                <TextBlock Text="Sally" />            </StackPanel>        </controls:TreeVIEwItem.header>        <controls:TreeVIEwItem header="John"/>        <controls:TreeVIEwItem header="Greg" IsExpanded="True">            <controls:TreeVIEwItem header="linda"/>            <controls:TreeVIEwItem header="Darren"/>        </controls:TreeVIEwItem>        <controls:TreeVIEwItem header="Allice" IsExpanded="True">            <controls:TreeVIEwItem header="Neil"/>        </controls:TreeVIEwItem>    </controls:TreeVIEwItem></controls:TreeVIEw>

We’ll repeat the process of editing an Image for the other 6 TreeVIEwItems. Than we’ll run our application.

 

here’s the XAML we wrote:

<controls:TreeVIEw FontSize="22" Height="350" WIDth="250" margin="84,0" VerticalAlignment="top" HorizontalAlignment="left">    <controls:TreeVIEwItem IsExpanded="True">        <controls:TreeVIEwItem.header>            <StackPanel OrIEntation="Horizontal">                <Image Source="AlIEn1.png" />                <TextBlock Text="Sally" />            </StackPanel>        </controls:TreeVIEwItem.header>                <controls:TreeVIEwItem>                    <controls:TreeVIEwItem.header>                        <StackPanel OrIEntation="Horizontal">                            <Image Source="AlIEn2.png" />                            <TextBlock Text="John" />                        </StackPanel>                    </controls:TreeVIEwItem.header>                </controls:TreeVIEwItem>                <controls:TreeVIEwItem IsExpanded="True">                    <controls:TreeVIEwItem.header>                        <StackPanel OrIEntation="Horizontal">                            <Image Source="AlIEn3.png" />                            <TextBlock Text="Greg" />                        </StackPanel>                    </controls:TreeVIEwItem.header>                    <controls:TreeVIEwItem>                        <controls:TreeVIEwItem.header>                            <StackPanel OrIEntation="Horizontal">                                <Image Source="AlIEn4.png" />                                <TextBlock Text="linda" />                            </StackPanel>                        </controls:TreeVIEwItem.header>                    </controls:TreeVIEwItem>                    <controls:TreeVIEwItem>                        <controls:TreeVIEwItem.header>                            <StackPanel OrIEntation="Horizontal">                                <Image Source="AlIEn5.png" />                                <TextBlock Text="Darren" />                            </StackPanel>                        </controls:TreeVIEwItem.header>                    </controls:TreeVIEwItem>                    </controls:TreeVIEwItem>        <controls:TreeVIEwItem IsExpanded="True">                    <controls:TreeVIEwItem.header>                        <StackPanel OrIEntation="Horizontal">                            <Image Source="AlIEn6.png" />                            <TextBlock Text="Allice" />                        </StackPanel>                    </controls:TreeVIEwItem.header>                    <controls:TreeVIEwItem>                        <controls:TreeVIEwItem.header>                            <StackPanel OrIEntation="Horizontal">                                <Image Source="AlIEn7.png" />                                <TextBlock Text="Neil" />                            </StackPanel>                        </controls:TreeVIEwItem.header>                    </controls:TreeVIEwItem>        </controls:TreeVIEwItem>    </controls:TreeVIEwItem></controls:TreeVIEw>

We can definitely see that we’ve dID some copy & paste here. Next we’ll see how we can use DataBinding to remove this repeatable code. 

 

 

Specifying a DataTemplate as the TreeVIEw’s ItemTemplate

Well,Now we’d like to remove that duplicated code by using some DataBinding.

We’ve got this AlIEn class:

public class AlIEn    {        public AlIEn(string name,string pictureUrl)        {            name = name;            Picture = new BitmAPImage(new Uri(pictureUrl,UriKind.relative));        }         public string name { get; set;  }        public BitmAPImage Picture { get; set; }    }

Pretty simple,we’ve got a name property,and a Picture property the we’re settings with way Silverlight uses for Image.source databinding.

We’ll jump back to Blend.

And clear all the Items from the TreeVIEwItem by selecting the first TreeVIEwItem and deleting it.

 

Now that we have an empty TreeVIEw,we’d like to set a x:name so we can setup the TreeVIEw‘sDataContext from our Code-behind.

 

We’ll set the TreeVIEw ItemSource to run on a collection of alIEns.

voID TreeVIEwPage_Loaded(object sender,RoutedEventArgs e){    trvAlIEns.ItemsSource = new List<AlIEn>()                                {                        new AlIEn("Sally","AlIEn1.png"),new AlIEn("John","AlIEn2.png"),new AlIEn("Greg","AlIEn3.png"),new AlIEn("linda","AlIEn4.png"),new AlIEn("Darren","AlIEn5.png"),new AlIEn("Alice","AlIEn6.png"),new AlIEn("Neil","AlIEn7.png"),};}

Now,we can finally get down to business – changing the TreeVIEw’s ItemTemplate.

Right Click on TreeVIEw –> Edit Other Templates –> Edit Generated Items (ItemTemplate) –> Create Empty.

We’ll call our new Template AlIEnTemplate.

We can see that we have an empty DataTemplate:

 

First,we’ll Change the GrID to a Horizontal StackPanel.

Right Click GrID –> Change Layout Type –> StackPanel.

 

 

Next we’ll add an Image control.

And we’ll want to DataBind it’s Source Property to AlIEn.Picture property.

Click Advanced Property options next to Source.

Select “Custom Expression”.

And put in “{Binding Picture}”.

 

Next we’ll add a TextBlock and Bind it’s Text property to “{Binding name}”.

-->

–>

-->

 

Let’s run this sample:

 

Here’s the XAML Blend Generated for our TreeVIEw:

<UserControl.Resources>    <DataTemplate x:Key="AlIEnTemplate">        <StackPanel OrIEntation="Horizontal">            <Image Source="{Binding Path=Picture}"/>            <TextBlock Text="{Binding Path=name}"/>        </StackPanel>    </DataTemplate></UserControl.Resources>Here’s our Code-behind:voID TreeVIEwPage_Loaded(object sender,};}

And our current AlIEn Class:
public class AlIEn{    public AlIEn(string name,string pictureUrl)    {        name = name;        Picture = new BitmAPImage(new Uri(pictureUrl,UriKind.relative));    }     public string name { get; set;  }    public BitmAPImage Picture { get; set; }} 

Specifying a HIErarchalDataTemplate as a TreeVIEw’s ItemTemplate in Visual Studio XAML Editor

In case you haven’t noticed,our current TreeVIEw is pretty flat. It only has 1 level. And we’d like to get a similar TreeVIEw to the one we prevIoUsly had,with nested TreeVIEwItems.

We’ll start by changing our CLR AlIEn Type:

    public class AlIEn    {        public AlIEn(string name,string pictureUrl,params AlIEn[] children)        {            name = name;            Picture = new BitmAPImage(new Uri(pictureUrl,UriKind.relative));           Children = children;        }         public string name { get; set; }        public BitmAPImage Picture { get; set; }       public AlIEn[] Children { get; set;  }    }

All we dID is add a property that is collection of AlIEns that are the Children of that AlIEn.

 

Now that we’ve got a HIErarchical AlIEn class,we’ll change our code behind to use it:

voID TreeVIEwPage_Loaded(object sender,RoutedEventArgs e){    trvAlIEns.ItemsSource = new List<AlIEn>()                        {                new AlIEn("Sally","AlIEn1.png","AlIEn3.png","AlIEn5.png")                            ),"AlIEn6.png","AlIEn7.png")                            )                          )                        };} 

The next part of changing our ItemTemplate is not supported by Blend. so we’re back at editing XAML in Visual Studio XAML Editor.

This is our current DataTemplate:

<DataTemplate x:Key="AlIEnTemplate">    <StackPanel OrIEntation="Horizontal">        <Image Source="{Binding Path=Picture}"/>        <TextBlock Text="{Binding Path=name}"/>    </StackPanel></DataTemplate>


Let’s change it to a HIErarchicalDataTemplateL:

<controls:HIErarchicalDataTemplate x:Key="AlIEnTemplate">    <StackPanel OrIEntation="Horizontal">        <Image Source="{Binding Path=Picture}"/>        <TextBlock Text="{Binding Path=name}"/>    </StackPanel></controls:HIErarchicalDataTemplate>


 

And we’ll need to point to our new HIErarchical CLR property – Children.

We’ll do that by Setting HIErarchicalDataTemplate.ItemsSource to “{Binding Children}”.

<controls:HIErarchicalDataTemplate x:Key="AlIEnTemplate" ItemsSource="{Binding Children}">    <StackPanel OrIEntation="Horizontal">        <Image Source="{Binding Path=Picture}"/>        <TextBlock Text="{Binding Path=name}"/>    </StackPanel></controls:HIErarchicalDataTemplate>


 

Let’s run the sample:

 

Here’s our XAML code for the TreeVIEw (hasn’t changed during this sample):

<controls:TreeVIEw FontSize="22" Height="350" WIDth="250" x:name="trvAlIEns" ItemTemplate="{StaticResource AlIEnTemplate}"/>Here’s our updated ItemTemplate:<UserControl.Resources>    <controls:HIErarchicalDataTemplate x:Key="AlIEnTemplate" ItemsSource="{Binding Children}">        <StackPanel OrIEntation="Horizontal">            <Image Source="{Binding Path=Picture}"/>            <TextBlock Text="{Binding Path=name}"/>        </StackPanel>    </controls:HIErarchicalDataTemplate></UserControl.Resources>


Here’s our updated AlIEn class:

public class AlIEn{    public AlIEn(string name,params AlIEn[] children)    {        name = name;        Picture = new BitmAPImage(new Uri(pictureUrl,UriKind.relative));        Children = children;    }     public string name { get; set; }    public BitmAPImage Picture { get; set; }    public AlIEn[] Children { get; set; }}


And our updated Code-behind:

voID TreeVIEwPage_Loaded(object sender,RoutedEventArgs e){    trvAlIEns.ItemsSource = new List<AlIEn>()                {        new AlIEn("Sally","AlIEn5.png")                    ),"AlIEn7.png")                    )                  )                };}
@H_502_760@ 总结

以上是内存溢出为你收集整理的Silverlight中TreeView,TreeViewItem,HierarchalDataTemplate的详细用法全部内容,希望文章能够帮你解决Silverlight中TreeView,TreeViewItem,HierarchalDataTemplate的详细用法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071672.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存