我正在尝试实现拖放。所以我需要在更晚的时间内开始拖曳操作。我怎样才能正确地把平庸的风格设置好呢?
看看我的代码
<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>我尝试在后面的代码、用户控件的视图模型( MyItems存在的地方)和MyItems的单个元素(一些数据类)中调用MyItems。
不幸的是,OnMouseDownStartDrag从未被调用过。我遗漏了什么?这种风格有什么问题吗?我应该把OnMouseDownStartDrag放在哪里,应该以什么方式调用它?
I使用WPF 4.6.2
发布于 2021-09-16 05:29:50
问题似乎不在于如何设置,也不在于根据这个EventHandler设置post的位置。
您应该尝试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>我已经尝试过从样式设置它的代码,它可以工作。
https://stackoverflow.com/questions/69202358
复制相似问题