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}" />
相关推荐
LDR0065 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术5 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园5 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob5 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享5 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.5 天前
C语言--day30
c语言·开发语言
何以解忧,唯有..5 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
謓泽5 天前
C语言不是语法,是通往机器的地图。
c语言·开发语言
云水一下5 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
飞天狗1115 天前
零基础JavaWeb入门——第五课第二小节:九大内置对象 · 第2个:response(响应对象)
java·开发语言