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}" />
相关推荐
2301_7634724613 分钟前
C++20概念(Concepts)入门指南
开发语言·c++·算法
TechWJ36 分钟前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
lly2024061 小时前
C++ 文件和流
开发语言
m0_706653231 小时前
分布式系统安全通信
开发语言·c++·算法
寻寻觅觅☆2 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++
lightqjx2 小时前
【C++】unordered系列的封装
开发语言·c++·stl·unordered系列
zh_xuan2 小时前
kotlin lazy委托异常时执行流程
开发语言·kotlin
阿猿收手吧!2 小时前
【C++】string_view:高效字符串处理指南
开发语言·c++
玄同7653 小时前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
Yorlen_Zhang3 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#