How can we activate a WPF trigger in XAML when performing a drag and drop operation?

We are looking for a way to base a WPF trigger in XAML on whether we are performing a drag and drop operation. Depending on whether we are or not, we want different behavior on hovering, so this is necessary.

The only way I can think of is to handle the drag start and end events and manually track the status, but this requires code, not pure XAML. Plus, it seems like a complete bust, especially since we have to do it for every potential fall target, which is real pain.

So, is there an easy way to say, “Hey ... I'm in drag and drop mode to activate this trigger,” or am I out of luck here?


Update

To clarify what we're trying to do now in pure XAML, you can create a style and then set a style trigger to examine the IsMouseOver property to draw a background selection. Well, we want to do this, but we want to say that if IsMouseOver is true and if IsDragging = true, then apply this trigger.

+3
source share
3 answers

I only had this problem, my solution consists of using an attached property that provides the missing one IsDragging:

  • Define an attached property

    public static readonly DependencyProperty IsDraggingProperty =
        DependencyProperty.RegisterAttached
        (
            "IsDragging",
            typeof(bool),
            typeof(ClassContainingThisProperty),
            new UIPropertyMetadata(false)
        );
    
    public static bool GetIsDragging(DependencyObject source)
    {
        return (bool)source.GetValue(IsDraggingProperty);
    }
    
    public static void SetIsDragging(DependencyObject target, bool value)
    {
        target.SetValue(IsDraggingProperty, value);
    }
    
  • Create these extension methods to help you set the property.

    public static TParent FindParent<TParent>(this DependencyObject child) where TParent : DependencyObject
    {
        DependencyObject current = child;
        while(current != null && !(current is TParent))
        {
            current = VisualTreeHelper.GetParent(current);
        }
        return current as TParent;
    }
    
    public static void SetParentValue<TParent>(this DependencyObject child, DependencyProperty property, object value) where TParent : DependencyObject
    {
        TParent parent = child.FindParent<TParent>();
        if(parent != null)
        {
            parent.SetValue(property, value);
        }
    }
    
  • DragDrop (, ListView), .

    private void OnDragEnter(object sender, DragEventArgs e)
    {
        DependencyObject source = e.OriginalSource as DependencyObject;
        if (source != null)
        {
            source.SetParentValue<ListViewItem>(ClassContainingTheProperty.IsDraggingProperty, true);
        }
    }
    
    private void OnDragLeave(object sender, DragEventArgs e)
    {
        DependencyObject source = e.OriginalSource as DependencyObject;
        if(source != null)
        {
            source.SetParentValue<ListViewItem>(ClassContainingTheProperty.IsDraggingProperty, false);
        }
    }
    
    private void OnDrop(object sender, DragEventArgs e)
    {
        DependencyObject source = e.OriginalSource as DependencyObject;
        if(source != null)
        {
            source.SetParentValue<ListViewItem>(ClassContainingTheProperty.IsDraggingProperty, false);
        }
    }
    
  • <ControlTemplate TargetType="{x:Type ListViewItem}">
      <ControlTemplate.Triggers>
        <Trigger Property="namespace:ClassContainingTheProperty.IsDragging" Value="True">
          <Setter Property="Background" Value="White" />
          <Setter Property="BorderBrush" Value="Black" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
    
+1

- , , , . , , .

0

if this is a real DragDrop event, just watch the DragOver event ...

            <EventTrigger RoutedEvent="DragOver">
                <BeginStoryboard>
                    <Storyboard Storyboard.TargetProperty="Fill.Color">
                        <ColorAnimation From="White" To="Black" Duration="0:0:1" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
0
source

Source: https://habr.com/ru/post/1779829/


All Articles