WPF DragLeave EventTrigger无法始终正常工作

时间:2017-12-18 17:00:22

标签: c# wpf eventtrigger

我遇到了一些用户控件的问题,我将它放在一起,这是一种用户可以放置对象的方向键,然后将新对象放在drop的方向上。我已经使用EventTriggers连接突出显示,它似乎在95%的时间内正常工作 - 例如。用户在控件上拖动一些东西,相应的控件突出显示。用户拖拽项目或将其放在突出显示的控件上,控件不会突出显示。问题在于,有时候,特别是当快速移动拖动的对象围绕多个方向控件时,某些控件无法失去高光。我根本没有在代码隐藏中做任何事情,唯一能驱动它的是这个xaml。

有什么想法吗?

CrossDropControl.xaml用户控制 - 无后面代码

<UserControl x:Class="EventTriggersDemo.CrossDropControl"
             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" 
             xmlns:local="clr-namespace:EventTriggersDemo"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="DirectionalBorderStyle"
               TargetType="Border">
            <Setter Property="Height" Value="28" />
            <Setter Property="Width" Value="28" />
            <Setter Property="Background" Value="#FF323232" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="AllowDrop" Value="True" />
            <Style.Triggers>
                <EventTrigger RoutedEvent="DragEnter">
                    <BeginStoryboard x:Name="DragEnterStoryboard">
                        <Storyboard>
                            <ColorAnimation Duration="0:0:0.1" 
                                            Storyboard.TargetProperty="Background.Color"
                                            To="LightBlue" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="DragLeave">
                    <StopStoryboard BeginStoryboardName="DragEnterStoryboard" />
                </EventTrigger>
                <EventTrigger RoutedEvent="Drop">
                    <StopStoryboard BeginStoryboardName="DragEnterStoryboard" />
                </EventTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="DirectionalShapeStyle"
               TargetType="Shape">
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Fill" Value="Gray" />
        </Style>

    </UserControl.Resources>
    <Grid>
        <Border Name="dropPopupControl"
                Width="98"
                Height="98">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Path x:Name="CompassBodyIcon"
                      Grid.RowSpan="3"
                      Grid.ColumnSpan="3"
                      Width="98"
                      Height="98"
                      Data="F1 M 0,32.0045L 21.0029,32.0045L 32.0045,21.0029L 32.0045,7.62939e-006L 66.0092,7.62939e-006L 66.0092,21.0029L 77.0107,32.0045L 98.0137,32.0045L 98.0137,66.0092L 77.0107,66.0092L 66.0092,77.0107L 66.0092,98.0137L 32.0045,98.0137L 32.0045,77.0107L 21.0029,66.0092L 0,66.0092L 0,32.0045 Z "
                      Fill="Gray"
                      SnapsToDevicePixels="True"
                      Stretch="Fill"
                      Stroke="Black" />
                <Border x:Name="PART_LeftIndicator"
                        Grid.Row="1"
                        Grid.Column="0"
                        Margin="2,0,0,0"
                        Style="{StaticResource DirectionalBorderStyle}">
                    <Path x:Name="LeftPlacementIndicator"
                          Data="F1 M 7.00043,13.0018L 22.0025,22.0031L 22.0025,4.00056L 7.00043,13.0018 Z "
                          Style="{StaticResource DirectionalShapeStyle}" />
                </Border>
                <Border x:Name="PART_RightIndicator"
                        Grid.Row="1"
                        Grid.Column="2"
                        Margin="0,0,2,0"
                        Style="{StaticResource DirectionalBorderStyle}">
                    <Path x:Name="RightHighlightVisual"
                          Data="F1 M 19.0025,13.0018L 4.00042,4.00056L 4.00042,22.0031L 19.0025,13.0018 Z "
                          Style="{StaticResource DirectionalShapeStyle}" />

                </Border>
                <Border x:Name="PART_BottomIndicator"
                        Grid.Row="2"
                        Grid.Column="1"
                        Margin="0,0,0,2"
                        Style="{StaticResource DirectionalBorderStyle}">
                    <Path x:Name="BottomPlacementIndicator"
                          Data="F1 M 12.9824,19.002L 21.9837,3.99995L 3.98116,3.99995L 12.9824,19.002 Z "
                          Style="{StaticResource DirectionalShapeStyle}" />
                </Border>
                <Border x:Name="PART_TopIndicator"
                        Grid.Column="1"
                        Margin="0,2,0,0"
                        Style="{StaticResource DirectionalBorderStyle}">
                    <Path x:Name="TopPlacementIndicator"
                          Data="F1 M 13.0022,7.021L 4.0009,22.0231L 22.0034,22.0231L 13.0022,7.021 Z "
                          Style="{StaticResource DirectionalShapeStyle}" />
                </Border>
                <Border x:Name="PART_CenterIndicator"
                        Grid.Row="1"
                        Grid.Column="1"
                        Margin="2"
                        Style="{StaticResource DirectionalBorderStyle}">
                    <Ellipse Margin="4"
                             Style="{StaticResource DirectionalShapeStyle}" />
                </Border>
            </Grid>
        </Border>
    </Grid>
</UserControl>

MainWindow.xaml

    <Window x:Class="EventTriggersDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:EventTriggersDemo"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="350" 
        Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <ListBox x:Name="dragListBox" PreviewMouseMove="UIElement_OnPreviewMouseMove">
            <ListBox.Items>
                <ListBoxItem>Item 1</ListBoxItem>
                <ListBoxItem>Item 2</ListBoxItem>
                <ListBoxItem>Item 3</ListBoxItem>
            </ListBox.Items>
        </ListBox>

        <local:CrossDropControl Grid.Column="1" />
    </Grid>
</Window>

// MainWindow.xaml.cs

    using System.Windows;
using System.Windows.Input;

namespace EventTriggersDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void UIElement_OnPreviewMouseMove(object sender, MouseEventArgs e)
        {
            // Get some mock drag data
            var dragData = new DataObject("Test Data");
            DragDrop.DoDragDrop(dragListBox, dragData, DragDropEffects.Move);
        }
    }
}

在准备此示例时我注意到,删除由Border控件包装的* PlacementIndicator路径似乎要么完全删除此问题,要么将其最小化到导致我无法重现它的程度。我不明白这会如何影响这种行为......

有什么想法吗?

0 个答案:

没有答案