Silverlight带关闭动画的内容控件,可移动的内容控件(二)
2011-08-14 22:49
218 查看
B.可移动的内容控件
此控件可以通过在鼠标左键按下的状态下同时拖动鼠标便可以移动控件,实现拖拽的效果。
先来看Xaml
View Code
在 OnApplyTemplate方法中获取moveBar 为UIElement元素,并为其添加鼠标点下,鼠标移动,鼠标放开事件。在鼠标点下时MoveBarELement.ReleaseMouseCapture();锁定鼠标至MoveBar上,鼠标移动时改变先前定义的panelTranslate,x值与y值,实现鼠标移动且元素跟着移动的效果。在鼠标放开是MoveBarELement.CaptureMouse();以释放对鼠标的锁定。
这样便可以定义一个能拖拽的面版!且面板样式可随大家任意改动!
此控件可以通过在鼠标左键按下的状态下同时拖动鼠标便可以移动控件,实现拖拽的效果。
先来看Xaml
View Code
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SuperMapStandardMapApp1 { public class FloatPanel : ContentControl { private bool isMouseDown; private Point mouseCurrentPoint; private TranslateTransform panelTranslate; public FloatPanel() { DefaultStyleKey = typeof(FloatPanel); } public override void OnApplyTemplate() { base.OnApplyTemplate(); RenderTransform = panelTranslate = new TranslateTransform(); UIElement moveBar = GetTemplateChild("MoveBar") as UIElement; //为移动面板,关闭面板提供事件 if (moveBar != null) { moveBar.MouseLeftButtonDown += new MouseButtonEventHandler(moveBar_MouseLeftButtonDown); moveBar.MouseLeftButtonUp += new MouseButtonEventHandler(moveBar_MouseLeftButtonUp); moveBar.MouseMove += new MouseEventHandler(moveBar_MouseMove); } } void moveBar_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { UIElement MoveBarELement = sender as UIElement; Point point = e.GetPosition(null); double x0 = this.panelTranslate.X; double y0 = this.panelTranslate.Y; this.panelTranslate.X = x0 + (point.X - mouseCurrentPoint.X); this.panelTranslate.Y = y0 + (point.Y - mouseCurrentPoint.Y); mouseCurrentPoint = point; } } void moveBar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { UIElement MoveBarELement = sender as UIElement; MoveBarELement.ReleaseMouseCapture(); isMouseDown = false; } void moveBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { UIElement MoveBarELement = sender as UIElement; mouseCurrentPoint = e.GetPosition(null); MoveBarELement.CaptureMouse(); isMouseDown = true; } public bool IsOpen { get { return (bool)GetValue(IsOpenProperty); } set { SetValue(IsOpenProperty, value); } } public static DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(FloatPanel), new PropertyMetadata(true, IsOpenPropertyChange)); public static void IsOpenPropertyChange(DependencyObject sender, DependencyPropertyChangedEventArgs e) { ((FloatPanel)sender).ChangeVisualState(true); } private void ChangeVisualState(bool IsChange) { if (IsOpen) { VisualStateManager.GoToState(this, "Open", IsChange); } else { VisualStateManager.GoToState(this, "Close", IsChange); } } } }
在 OnApplyTemplate方法中获取moveBar 为UIElement元素,并为其添加鼠标点下,鼠标移动,鼠标放开事件。在鼠标点下时MoveBarELement.ReleaseMouseCapture();锁定鼠标至MoveBar上,鼠标移动时改变先前定义的panelTranslate,x值与y值,实现鼠标移动且元素跟着移动的效果。在鼠标放开是MoveBarELement.CaptureMouse();以释放对鼠标的锁定。
这样便可以定义一个能拖拽的面版!且面板样式可随大家任意改动!
相关文章推荐
- silverlight中 Storyboard(动画)的使用,实现球的上下循环移动,左右移动,及旋转功能
- javascript select控件间内容互相移动
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(六)完美移动
- 属性动画实现控件类似贝塞尔曲线轨迹移动效果
- 属性动画实现控件类似贝塞尔曲线轨迹移动效果
- 利用Timer和图片框控件,编写一个图片不断向左移动的小动画
- C#中,当鼠标移动到控件上,动态显示提示内容 ToolTip
- silverlight控件动画状态的过渡
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十一)主位式地图移动模式
- UIDatePicker 控件获取本地时间 和 UIDatePicker的移出移回(简单的动画移动效果)
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(十三)牵引式地图移动模式①
- ActiveReport的ActiveX模式下,打开页面后,控件不显示内容,右上角扫描的动画显示在读取什么的问题
- 属性动画实现控件类似贝塞尔曲线轨迹移动效果
- 手把手教你使用Silverlight实现3D场景二(创建自定义模版控件为物件应用3D动画)
- [原]Silverlight两种自定义包含内容(Content)控件的方案
- WPF,Silverlight与XAML读书笔记第二十一 - 控件之三 – 内容控件之选择控件&信息显示控件
- Windows 7个性化配置,关闭Win7动画效果,设置窗口背景为“ 豆绿色”,移动“我的文档”
- IOS 控件带动画移动
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(十三)牵引式地图移动模式①
- android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果