在Silverlight里实现控件上下移动

界面文件:
<UserControl x:Class="SilverlightApplication1.MainPage"
    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" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
   <StackPanel HorizontalAlignment="Left" Margin="48,8,0,41" Width="99" Background="Black" Name="statxt">
    <TextBox Text="TextBox1" TextWrapping="Wrap" GotFocus="TextBox_GotFocus"/>
    <TextBox Text="TextBox2" TextWrapping="Wrap" GotFocus="TextBox_GotFocus" />
    <TextBox Text="TextBox3" TextWrapping="Wrap" GotFocus="TextBox_GotFocus" />
    <TextBox Text="TextBox4" TextWrapping="Wrap" GotFocus="TextBox_GotFocus"/>
   </StackPanel>
   <Button Height="27" Margin="192,202,303,0" VerticalAlignment="Top" Content="向下移" Click="Button_Click" Tag="MoveDown"/>
   <Button Height="29" Margin="174,68,303,0" VerticalAlignment="Top" Content="向上移" Click="Button_Click" Tag="MoveUp"/>
  </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 SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        TextBox oldtxt = null;
        int oldIndex = 0;
        public MainPage()
        {
            InitializeComponent();
        }
        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            oldtxt = (TextBox)sender;
             oldIndex = statxt.Children.IndexOf(oldtxt);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
                int currentIndex = -1;
                TextBox t = null;
                Button btn = (Button)sender;
                switch (btn.Tag.ToString())
                {
                    case "MoveUp":
                        if (oldIndex > 0)
                        {
                            currentIndex = oldIndex - 1;
                            statxt.Children.RemoveAt(oldIndex);
                            statxt.Children.Insert(currentIndex, oldtxt);
                            t = (TextBox)statxt.Children[currentIndex];
                            t.Focus();
                            oldIndex = currentIndex;
                        }
                        break;
                    case "MoveDown":
                        if (oldIndex < statxt.Children.Count - 1)
                        {
                            currentIndex = oldIndex + 1;
                            t = (TextBox)statxt.Children[currentIndex];
                            statxt.Children.RemoveAt(currentIndex);
                           // statxt.Children.RemoveAt(oldIndex);
                            statxt.Children.Insert(oldIndex, t);
                            //statxt.Children.Insert(currentIndex, oldtxt);
                            t = (TextBox)statxt.Children[currentIndex];
                            t.Focus();
                            oldIndex = currentIndex;
                        }
                        break;
                }
                if (oldIndex == 0 || oldIndex == statxt.Children.Count - 1)
                {
                    t = (TextBox)statxt.Children[oldIndex];
                    t.Focus();
                }
        }
    }
}
 

 图片显示的是:几个TextBox根据Button命令,上下移动

 

 

 

作者: 风云

出处: http://www.cnblogs.com/fengyunlishi/

本文版权归风云和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

 

posted @ 2012-05-28 08:25  风云@历史  阅读(296)  评论(0编辑  收藏  举报