7

I was trying to add some visual feed back for a list box that supports drag and drop. Seems like I should be able to add some setters to an EventSetter and be done. However, eventsetters don't support setters. Do I really have to make a storyboard to implement this behavior?

What is Microsoft's rational for this?

   <Style TargetType="{x:Type ListBox}">
        <Style.Triggers>
             <EventTrigger RoutedEvent="DragEnter">
                 <!--WHy Can't i Add seters here? e.g.
                <Setter Property="ForeColor" Value="Red"> 
                -->
            </EventTrigger>
        </Style.Triggers>
    </Style>
PeterM
  • 2,372
  • 1
  • 26
  • 35

4 Answers4

8

A setter doesn't just set a property in response to state changing, it also restores the property to its previous value when the state changes back. There's no "changes back" with event triggers, so using setters with them would be like pushing something onto a stack that never gets popped.

I think a much better question, under the circumstances, is "why isn't there an IsDragOver property?"

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
3

You could, maybe, try something like this? This is sorta-pseudo code, I don't have VS here to test it, but it should work. Using reflector you should be able to reverse engineer SetterAction and have this Setter work almost exactly the same way, in theory.

<TextBox Text="ListBox" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="DragEnter" >
            <behavior:SetterAction Property="ListBox.ForeColor" Value="Red"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

public class SetterAction : TargetedTriggerAction<FrameworkElement>
{       
    public DependencyProperty Property { get; set; }
    public Object Value { get; set; }   


    protected override void Invoke(object parameter)
    {                 
        AssociatedObject.SetValue(Property, Value);       
    }
}
Lugoues
  • 424
  • 3
  • 5
  • 1
    Hi Lugoues. Can you Explain more about your solution ? what is TargetedTriggerAction is it a .net Class ? i find this page https://msdn.microsoft.com/en-us/library/ff726545(v=expression.40).aspx but i cannot find System.Windows.Interactivity in assemblies – Mamad Mar 05 '17 at 11:51
  • @Mamad you can find the System.Windows.Interactivity assembly in the blend sdk. http://www.nuget.org/packages/Expression.Blend.Sdk/ – Lugoues Mar 07 '17 at 00:40
  • +1 for the nice alternative to using Storyboard & behaviors. Here I think the simple enough code behind is worth the verbose storyboard XAML. – VeV Nov 13 '17 at 23:46
1

You can't put setters in eventtriggers. Thats just how it is. You dont need a storyboard if you don't mind doing some code behind work. Tons of others with the same problem so don't feel too bad.

check out How to open a WPF Popup when another control is clicked, using XAML markup only?

Community
  • 1
  • 1
Dustin Davis
  • 14,482
  • 13
  • 63
  • 119
1

Yeah, it sucks. Same issue here EventTrigger with Setter in WPF? they ended up using a storyboard. It isn't much worse than a setter.

Community
  • 1
  • 1
Dustin Davis
  • 14,482
  • 13
  • 63
  • 119