首页 新闻 搜索 专区 学院

WPF Treeview怎么实现同一层的节点只能展开一个节点???

0
悬赏园豆:5 [已解决问题] 解决于 2013-03-12 15:24
 public class TreeNode 
    {
        public string Name { get; set; }
        public string Icon { get; set; }
        public bool IsNodeExpanded { get; set; }
        public List<TreeNode> Children { get; set; }

        public TreeNode()
        {
            Children = new List<TreeNode>();
        }
    }

上面是节点定义;

下面是树定义:

 1 <TreeView Name="tree" Margin="1,1,1,1" Opacity="1">
 2                 <TreeView.Background>
 3                     <ImageBrush ImageSource="/ImageTree;component/bin/Debug/Resource/VideoBackground.jpg" Stretch="UniformToFill" TileMode="None" />
 4                 </TreeView.Background>
 5                 <TreeView.ItemContainerStyle>
 6                 <Style TargetType="TreeViewItem">
 7                     <Setter Property="IsExpanded" Value="{Binding IsNodeExpanded}"></Setter>
 8                 </Style>
 9             </TreeView.ItemContainerStyle>
10             <TreeView.ItemTemplate>
11                 <HierarchicalDataTemplate DataType="{x:Type local:TreeNode}" ItemsSource="{Binding Path=Children}">
12                     <StackPanel Orientation="Horizontal">
13                         <Image Name="image"  Source="{Binding Icon}"  Margin="2"/>
14                         <TextBlock Text="{Binding Name}" Margin="2" FontWeight="Bold" FontSize="14" Foreground="Black" />
15                     </StackPanel>
16                 </HierarchicalDataTemplate>
17             </TreeView.ItemTemplate>
18         </TreeView>
Raylin的主页 Raylin | 菜鸟二级 | 园豆:246
提问于:2013-03-11 16:29
< >
分享
最佳答案
0

展开一个节点之后,其他同级节点都要收起来?

收获园豆:5
陈希章 | 老鸟四级 |园豆:2338 | 2013-03-11 17:51

嗯,我想法是先全部关闭,然后开启将当前选择的节点的IsNodeExpanded=true,但是没有效果,代码如下:

View Code
 1    public void CompareTree(TreeNode Root,TreeNode TargetNode)
 2         {
 3             foreach (TreeNode Node in Root.Children)
 4             {
 5                 if (TargetNode.Name != Node.Name)
 6                 {
 7                     Node.IsNodeExpanded = false;
 8                 }
 9                 else  
10                     break; 
11                 CompareTree(Node, TargetNode);
12             }
13             TargetNode.IsNodeExpanded = true;
14         }
Raylin | 园豆:246 (菜鸟二级) | 2013-03-12 08:48

@Raylin: 给你做一个简单的例子,你在此基础上再扩展一下吧

我写了一个自定义的TreeViewItem

using System.Linq;
using System.Windows.Controls;

namespace WpfApplication1
{
    class MyTreeViewItem : TreeViewItem
    {
        public MyTreeViewItem()
        {
            Expanded += MyTreeViewItem_Expanded;
        }

        void MyTreeViewItem_Expanded(object sender, System.Windows.RoutedEventArgs e)
        {

            var query = (this.Parent as TreeView).Items.OfType<MyTreeViewItem>().Where(t => t != this);
            foreach (var item in query)
            {
                item.IsExpanded = false;
            }

        }


        public int Level { get; set; }

    }
}

在窗体上面这样使用

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Grid>
        <TreeView x:Name="tv">
            <local:MyTreeViewItem Level="1"
                                  Header="A">
                <TreeViewItem Header="A1"></TreeViewItem>
                <TreeViewItem Header="A2"></TreeViewItem>
                <TreeViewItem Header="A3"></TreeViewItem>
                <TreeViewItem Header="A4"></TreeViewItem>
            </local:MyTreeViewItem>
            <local:MyTreeViewItem Level="1"
                                  Header="B">
                <TreeViewItem Header="B1"></TreeViewItem>
                <TreeViewItem Header="B2"></TreeViewItem>
                <TreeViewItem Header="B3"></TreeViewItem>
                <TreeViewItem Header="B4"></TreeViewItem>
            </local:MyTreeViewItem>
        </TreeView>
    </Grid>
</Window>
陈希章 | 园豆:2338 (老鸟四级) | 2013-03-12 11:58

@陈希章: 如果我继承了TreeViewItem了,我节点的图片就显示不出来了,刚接触WPF,不知道怎么解决

Raylin | 园豆:246 (菜鸟二级) | 2013-03-12 15:39
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Height="350"
        Width="525">

    <Window.Resources>
        <Style TargetType="local:MyTreeViewItem">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="rss-icon.png"></Image>
                            <TextBlock Text="{Binding}"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <TreeView x:Name="tv">
            <local:MyTreeViewItem Level="1"
                                  Header="A">
                <TreeViewItem Header="A1"></TreeViewItem>
                <TreeViewItem Header="A2"></TreeViewItem>
                <TreeViewItem Header="A3"></TreeViewItem>
                <TreeViewItem Header="A4"></TreeViewItem>
            </local:MyTreeViewItem>
            <local:MyTreeViewItem Level="1"
                                  Header="B">
                <TreeViewItem Header="B1"></TreeViewItem>
                <TreeViewItem Header="B2"></TreeViewItem>
                <TreeViewItem Header="B3"></TreeViewItem>
                <TreeViewItem Header="B4"></TreeViewItem>
            </local:MyTreeViewItem>
        </TreeView>
    </Grid>
</Window>

@Raylin: 

陈希章 | 园豆:2338 (老鸟四级) | 2013-03-12 18:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册