Silverlight:MouseDragElementBehavior无法应用于ListBox的变相解决办法
2011-08-16 08:41
246 查看
Blend自带的行为MouseDragElementBehavior应用到ListBox后,如果用鼠标按住列表列拖动,没有任何效果,在多次尝试中意外发现,如果将ListBox的边框设置成一个较大值,在边框上点击时,却可以拖动,但是一般开发中,没人会把ListBox设置一个粗粗的难看边框。于是想到了下面的变通解决办法:当鼠标进入时显示边框,鼠标离开时再隐藏边框。
示例代码:
Xaml部分
Xaml.cs部分
示例代码:
Xaml部分
<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:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" x:Class="sl_drag_sample.MainPage" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <ListBox x:Name="lbSample" MinHeight="50" MinWidth="100" BorderThickness="0" HorizontalAlignment="Center" VerticalAlignment="Center" MouseEnter="ShowBorder" MouseLeftButtonUp="HideBorder" MouseLeftButtonDown="ShowBorder" MouseMove="ShowBorder" MouseLeave="HideBorder"> <i:Interaction.Behaviors> <ei:MouseDragElementBehavior/> </i:Interaction.Behaviors> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="这是测试文字"> <i:Interaction.Behaviors> <ei:MouseDragElementBehavior/> </i:Interaction.Behaviors> </TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </UserControl>
Xaml.cs部分
using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace sl_drag_sample { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { char[] s = "ABCDE".ToCharArray(); lbSample.ItemsSource = s; } private void ShowBorder(object sender, MouseEventArgs e) { (sender as ListBox).BorderThickness = new Thickness(20.0); } private void HideBorder(object sender, MouseEventArgs e) { (sender as ListBox).BorderThickness = new Thickness(0.0); } } }
相关文章推荐
- silverlight 拖动实现-MouseDragElementBehavior
- 非网络引用element-ui css导致图标无法正常显示的解决办法
- silverlight MediaElement创建之后不能播放解决办法
- silverlight缓存无法更新的简易解决办法
- 在Win7下无法安装Silverlight的解决办法
- VS2008创建Silverlight项目出错的解决办法--安装Visual Studio 2008的Silverlight 1.1 Tools Alpha无法创建Silverlight项目
- 零元学Expression Blend 4 – Chapter 21 以实作案例学习MouseDragElementBehavior
- Silverlight MediaElement创建之后不能播放解决办法
- [无法计算项元数据“%(FullPath)” 无法将项元数据“%(FullPath)”应用于路径] 问题解决办法
- Silverlight无法启动调试,错误“Unable to start debugging. The Silverlight Developer Runtime is not installed. Please install a matching version.” 解决办法
- 在vs2010中,开发silverlight时无法感知和加载样式的解决办法。
- "2104 无法下载Silverlight应用程序" 错误解决办法
- silverlight无法安装的解决办法
- Silverlight 2 beta 2无法创建项目的解决办法
- Silverlight Textbox在失去焦点之前无法获取到Text值的解决办法
- silverlight无法命中断点解决办法
- 在Silverlight 4下ASP.NET 网站管理工具无法连接的解决办法
- Silverlight在IE中无法显示但在Firefox中正常的原因和解决办法
- VS2008创建Silverlight项目出错的解决办法--安装Visual Studio 2008的Silverlight 1.1 Tools Alpha无法创建Silverlight项目
- 更新Silverlight 后 无法启动调试 未安装Silverlight developer 运行时解决办法