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}" />
相关推荐
weixin_3077791327 分钟前
PySpark实现ABC_manage_channel逻辑
开发语言·python·spark
??? Meggie1 小时前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
酷爱码3 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼3 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
喵先生!4 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ5 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics5 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
yuanManGan7 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++
北极的企鹅887 小时前
XML内容解析成实体类
xml·java·开发语言
BillKu7 小时前
Vue3后代组件多祖先通讯设计方案
开发语言·javascript·ecmascript