views:

422

answers:

2

With DataTriggers in WPF its possible to set properties on controls based on the the object you have bound. For example you could set the Background of a TextBlock based on a IsAlive property on your object.

<DataTrigger Binding="{Binding Path=IsAlive}" Value="true">
    <Setter Property="Background" Value="Yellow"/>
</DataTrigger>

I want to know if its possible to go in reverse. Is it possible to set a property on a databound item based on the state of the control its bound to?

Say I want to set the IsAlive property to true when the control its bound to receives the mouseover event.

Can this be done in WPF & data triggers? Thanks.

+1  A: 

I don't know if what you're asking is directly possible, but I suspect it isn't. On the other hand, I think you could make your example scenario work by binding the object's "IsAlive" property directly to the control's "IsMouseOver" dependency property, with Mode=OneWayToSource.

Daniel Pratt
+1 That sounds like it would work.
Pwninstein
A: 

You might want to use EventSetter, then handle the setting by code using the DataContext property of the sender, or with GetBindingExpression.
This gives you an option to set a handler on the style level.

Shimmy