silverlight 动态加载树形菜单[带图标],方法一

 

 

上代码了......

 

页面代码:

自定义样式:

 MouseLeftButtonUp事件是我用于末级子节点的点击事件(连续点击)自定义的,因为silverlight的tree没有点击事件,当然你可以使用treeViewItem的Selected事件处理,个人感觉比较麻烦。

个人习惯不同,自己选择处理方式。

<UserControl.Resources>
        <common:HierarchicalDataTemplate x:Key="Level3Template">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Icon}" Width="16" Height="16"/>
                <TextBlock Text="{Binding Name}"  Foreground="Black" MouseLeftButtonUp="TextBlock_MouseLeftButtonUp"/>
            </StackPanel>
        </common:HierarchicalDataTemplate>
        <common:HierarchicalDataTemplate x:Key="Level2Template" ItemsSource="{Binding Level3s}" ItemTemplate="{StaticResource Level3Template}">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Icon}" Width="16" Height="16"/>
                <TextBlock Text="{Binding Name}" Foreground="Green"/>
            </StackPanel>
        </common:HierarchicalDataTemplate>
        <common:HierarchicalDataTemplate x:Key="Level1Template" ItemsSource="{Binding Level2s}" ItemTemplate="{StaticResource Level2Template}">
            <TextBlock Foreground="Blue"/>
        </common:HierarchicalDataTemplate>
    </UserControl.Resources>
tree目录:
我这里只放了个根节点
<Grid Grid.Column="0" Grid.Row="0" VerticalAlignment="Stretch">
                    <controls:TreeView x:Name="myTree" BorderBrush="White"   ItemTemplate="{StaticResource Level1Template}">
                        <controls:TreeViewItem x:Name="treeItem" Header="XXX管理系统" Foreground="Blue" IsExpanded="True" ItemTemplate="{StaticResource Level2Template}">
                        </controls:TreeViewItem>
                    </controls:TreeView>
                </Grid>

后台代码:

自定义类:

 #region 自定义类
    public class Level1
    {
        public Level1()
        {
            Level2s = new ObservableCollection<Level2>();
        }
        public string Name { get; set; }
        public ObservableCollection<Level2> Level2s { get; set; }
    }
    public class Level2
    {
        public Level2()
        {
            Level3s = new ObservableCollection<Level3>();
        }
        public string Name { get; set; }
        public string Icon { get; set; }
        public ObservableCollection<Level3> Level3s { get; set; }
    }
    public class Level3
    {
        public string Name { get; set; }
        public string Icon { get; set; }
        //public event EventHandler click;
    }
    #endregion
 
加载树形:
 
/// <summary>
        /// 初始化目录数据
        /// </summary>
        private void InitTreeData()
        {
            treeItem.ItemsSource = new ObservableCollection<Level2> { 
                new Level2 
                  { 
                    Name="基础信息", Icon = "../ICON/project.png",
                    Level3s = { 
                        new Level3 { Name = "类别" ,Icon = "../ICON/sf.png"}, 
                        new Level3 { Name = "部门" ,Icon = "../ICON/app.png"},
                        new Level3 { Name = "类别2" ,Icon = "../ICON/contractType.png"},
                        new Level3 { Name = "部门2" ,Icon = "../ICON/post_to_blog.png"}
                    }
                  },
                  new Level2 
                  { 
                    Name="报表管理", Icon = "../ICON/report.png",
                    Level3s = { 
                        new Level3 { Name = "报表1" ,Icon = "../ICON/tabs.png"},
                        new Level3 { Name = "报表2" ,Icon = "../ICON/table49.png"},
                        new Level3 { Name = "报表3" ,Icon = "../ICON/table48.png"},
                        new Level3 { Name = "报表4",Icon="../ICON/report.png"}
                    }
                  },
                  new Level2 
                  { 
                    Name="系统管理",Icon = "../ICON/set.png",
                    Level3s = { 
                        new Level3 { Name = "权限设置" ,Icon = "../ICON/tick.png"},
                        new Level3 { Name = "用户管理" , Icon = "../ICON/users.png"}
                    }
                  }
            };
      }
 

加载菜单(tree)的方式很多,在这里我只提到了这一种,仅供参考!

 附件 :

 /Files/northstar/Silverlight树形菜单带图标.rar

 

posted on 2012-08-04 22:02  裴银祥的博客园  阅读(2346)  评论(0编辑  收藏  举报