现在有个需求,就是把silverlight treeview 同级的节点能够通过Button 进行向上向下调整

  如果有此类需求,就参考吧。


<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"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="tree节点顺序.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="277*"/>
<ColumnDefinition Width="123*"/>
</Grid.ColumnDefinitions>

<sdk:TreeView x:Name="TreeOfLife"/>
<Button x:Name="up" Content="向上" HorizontalAlignment="Left" Margin="7.948,46,0,0" VerticalAlignment="Top" Width="82" Height="45" RenderTransformOrigin="0.5,0.5" Click="up_Click" Grid.Column="1">

</Button>
<Button x:Name="down" Content="向下" HorizontalAlignment="Left" Margin="7.948,130,0,0" VerticalAlignment="Top" Width="84" RenderTransformOrigin="0.57,0.348" Height="56" Click="down_Click" Grid.Column="1"/>

</Grid>
</UserControl>



  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace tree节点顺序
{
public partial class MainPage : UserControl
{
#region 填充树
public class ForumInfo
{
public int ForumID { get; set; }
public int ParendID { get; set; }
public string ForumName { get; set; }
}
List<ForumInfo> forumList = new List<ForumInfo>();
public MainPage()
{
InitializeComponent();
GetForumData();
AddTreeNode(0, null);
}
public void GetForumData()
{

forumList.Add(new ForumInfo() { ForumID = 1, ParendID = 0, ForumName = "笔记本版块" });
forumList.Add(new ForumInfo() { ForumID = 2, ParendID = 0, ForumName = "台式机版块" });

forumList.Add(new ForumInfo() { ForumID = 3, ParendID = 1, ForumName = "Dell笔记本" });
forumList.Add(new ForumInfo() { ForumID = 4, ParendID = 1, ForumName = "IBM笔记本" });
forumList.Add(new ForumInfo() { ForumID = 5, ParendID = 1, ForumName = "IBM-T系列" });
forumList.Add(new ForumInfo() { ForumID = 6, ParendID = 1, ForumName = "IBM-R系列" });

forumList.Add(new ForumInfo() { ForumID = 7, ParendID = 2, ForumName = "联想台式机" });
forumList.Add(new ForumInfo() { ForumID = 8, ParendID = 2, ForumName = "方正台式机" });
forumList.Add(new ForumInfo() { ForumID = 9, ParendID = 2, ForumName = "HP台式机" });
forumList.Add(new ForumInfo() { ForumID = 10, ParendID = 2, ForumName = "联想家悦H系列" });
forumList.Add(new ForumInfo() { ForumID = 11, ParendID = 2, ForumName = "联想IdeaCentre系列" });


}

private void AddTreeNode(int parentID, TreeViewItem treeViewItem)
{
List<ForumInfo> result = (from forumInfo in forumList
where forumInfo.ParendID == parentID
select forumInfo).ToList<ForumInfo>();

if (result.Count > 0)
{
foreach (ForumInfo foruminfo in result)
{
TreeViewItem objTreeNode = new TreeViewItem();
objTreeNode.Header = foruminfo.ForumName;
objTreeNode.DataContext = foruminfo;


if (treeViewItem == null)
{
TreeOfLife.Items.Add(objTreeNode);
}
else
{
treeViewItem.Items.Add(objTreeNode);
}
AddTreeNode(foruminfo.ForumID, objTreeNode);
}
}
}

#endregion

/// <summary>
/// 调整节点顺序
/// </summary>
/// <param name="updown">向上-1 向下+1</param>
/// <param name="TreeOfLife">树实例</param>
private void treeitemupdown(int updown, TreeView TreeOfLife)
{
System.Windows.Controls.TreeViewItem SelectedItem = TreeOfLife.SelectedItem as TreeViewItem;

System.Windows.Controls.TreeViewItem item = SelectedItem;
if (SelectedItem != null)
{
System.Windows.Controls.TreeViewItem itemParent = item.Parent as TreeViewItem;
System.Windows.Controls.TreeViewItem itemNewParent;
int index;
if (itemParent == null)//如果树本身无根节点选中分类
{

index = TreeOfLife.Items.IndexOf(item);


if (index<0 || index + updown < 0 || index + updown >= TreeOfLife.Items.Count)
{
return;
}
TreeOfLife.Items.Remove(item);
TreeOfLife.Items.Insert(index + updown, item);
}
else
{
index = itemParent.Items.IndexOf(item);

if (index < 0 || index + updown < 0 || index + updown >= itemParent.Items.Count)
{
return;
}
itemParent.Items.Remove(item);
itemParent.Items.Insert(index + updown, item);


}

item.IsSelected = true;
}
}





private void up_Click(object sender, RoutedEventArgs e)
{
treeitemupdown(-1,TreeOfLife);
}

private void down_Click(object sender, RoutedEventArgs e)
{
treeitemupdown(1, TreeOfLife);
}
}
}