ListBoxItem 在样式中设置 EventSetter

约翰杜123

我正在尝试实现拖放。所以我需要在事件处理程序中开始拖动操作。如何正确设置事件处理程序的样式?

看看我的代码

<UserControl x:Class="MyProject.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              
             mc:Ignorable="d">
        
        <ListBox ItemsSource="{Binding MyItems}"  DisplayMemberPath="Name">

            <ListBox.ItemContainerStyle>
                
                <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">

                    <EventSetter Event="MouseDown"                       Handler="OnMouseDownStartDrag" />

                </Style>
                
            </ListBox.ItemContainerStyle>

        </ListBox>
           
</UserControl>

我试图在后面的代码中调用OnMouseDownStartDrag,在用户控件的视图模型中,其中存在MyItems以及MyItems的单个元素(某些数据类)。

不幸的是,从未调用OnMouseDownStartDrag我错过了什么?风格有问题吗?我应该在哪里放置OnMouseDownStartDrag以及应该以哪种方式调用它?

我使用 WPF 4.6.2

公斤

根据这篇文章,问题似乎不在于您如何设置或在哪里设置 EventHandler

您应该尝试使用PreviewMouseDown而不是MouseDown所以,它会像

<UserControl x:Class="MyProject.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              
         mc:Ignorable="d">
    
    <ListBox ItemsSource="{Binding MyItems}"  DisplayMemberPath="Name">

        <ListBox.ItemContainerStyle>
            
            <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">

                <EventSetter Event="PreviewMouseDown"                       Handler="OnMouseDownStartDrag" />

            </Style>
            
        </ListBox.ItemContainerStyle>

    </ListBox>
       

我已经尝试过从样式设置它的代码并且它有效。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Firemonkey应用程序中的自定义ListboxItem样式-Delphi 10.1 Berlin

WPF:不能在样式的目标标签上指定事件“完成”。改用EventSetter

在C#中删除listBoxItem时,ListBox不刷新

在ListBoxItem DataTemplate内部的嵌套控件中处理相同的click事件-WPF

访问ListboxItem中的控件

如何在UWP中更改ListBoxItem的选择突出显示颜色?

传输ListboxItem并在Hashtable中解决问题

UWP奇数和偶数元素ListBoxItem的不同样式

如何使用ToggleButton更改ListBoxItem中的命令

WPF中的ListBoxItem-选定时缩放动画

需要多种样式取决于Listboxitem

在绑定的列表框中禁用对listboxitem的拖放;集合已被修改的异常

<ListBox.Resources>或<ListBox.ItemContainerStyle>中的ListBoxItem样式?

ListBoxItem Delphi XE5中的项目顺序错误

将SelectedIndex设置为-1后,如何选择已选择的ListBoxItem或突出显示先前选择的ListBoxItem?

如何在Windows Phone 8.1中指定所选ListBoxItem的样式?

在WPF中,什么将ListBoxItem与ListBox相关联?

WPF ListBoxItem不会拉伸到wrappanel中的最大宽度

在“ FrameworkElement.Loaded”之后和“ Unloaded”之前播放动画(在ListBoxItem中)

UWP:在C#代码中更改ListBoxItem元素的(文本)样式

在ListBoxItem上的ViewModel中触发一个事件,单击

获取MouseOver上ListBoxItem的内容以出现在其他控件中

在运行时动态地将EventSetter添加到现有的分层数据模板中

如何将ListboxItem绑定为WPF中的可观察集合,并在顶部插入新记录

在 ResourceDictionary 中定义 Style,但在本地定义 EventSetter

我如何在 style->eventsetter 中处理我的自定义事件?

如何在 ListBox 中绑定和显示 ListBoxItem 索引?

在 WPF 中水平和垂直居中 ListBoxItem 内容

如何在 XAML 中更改 MaterialDesign ListBoxItem 的背景?(WPF)