WPF EventSetter 写法

感觉这玩意之前一直没用过,可能在容器里用到的比较多吧,记录一下。

第一种代码法:

cs 复制代码
      Style itemContainerStyle = new Style(typeof(ListBoxItem));
            itemContainerStyle.Setters.Add(new Setter(ListBoxItem.AllowDropProperty, true));
            itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(s_PreviewMouseLeftButtonDown)));
            itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.DropEvent, new DragEventHandler(listbox1_Drop)));
            listbox1.ItemContainerStyle = itemContainerStyle;

第二种xaml法:

XML 复制代码
<ListBox>
    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="AllowDrop" Value="True" />
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="s_PreviewMouseLeftButtonDown" />
            <EventSetter Event="Drop" Handler="listbox1_Drop" />
        </Style>
    </ListBox.Resources>
</ListBox>

如果使用MVVM框架可以使用Binding:

XML 复制代码
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="{Binding s_PreviewMouseLeftButtonDown}" />
            <EventSetter Event="Drop" Handler="{Binding listbox1_Drop}" />
相关推荐
互联网中的一颗神经元7 小时前
小白python入门 - 39. 采集流水线小项目
开发语言·python
Wang's Blog7 小时前
Go-Zero 项目开发22:用户群聊功能的实现与完善
开发语言·golang
a1117768 小时前
坦克大战3D Three.js 3D (开源项目)
开发语言·javascript·3d
Wang's Blog10 小时前
Go-Zero项目开发24: 基于Bitmap实现群聊消息已读未读
开发语言·后端·golang
吃好睡好便好12 小时前
MATLAB中图像的读取、写入和显示
开发语言·图像处理·学习·计算机视觉·matlab
yaoxin52112312 小时前
476. Java 反射 - 调用方法
java·开发语言
猫头虎13 小时前
什么是ZCode for GLM-5.2?
开发语言·人工智能·python·科技·算法·ai编程·ai写作
bksczm14 小时前
Linux之日志和线程池、内存池
java·开发语言
笨蛋不要掉眼泪14 小时前
Java虚拟机:对象复活、引用强度与Stop-The-World
java·开发语言·jvm
code_pgf14 小时前
C/C++ 常用容器功能汇总
c语言·开发语言·c++