Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Can't I put a Setter Inside an Event Trigger

Tags:

.net

styles

wpf

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>
like image 971
PeterM Avatar asked Mar 03 '11 01:03

PeterM


2 Answers

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?"

like image 73
Robert Rossney Avatar answered Oct 27 '22 20:10

Robert Rossney


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);       
    }
}
like image 23
Lugoues Avatar answered Oct 27 '22 22:10

Lugoues