WPF Data Triggers Set Event-Based Object Properties

With DataTriggers in WPF, you can set properties on controls based on a related object. For example, you can set the TextBlock background based on the IsAlive property on your object.

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

I want to know if it is possible to return in the opposite direction. Is it possible to set a property on a database item based on the state of the control associated with it?

Let's say I want to set the IsAlive property to true when the control is bound to a mouseover event.

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

+4
source share
2 answers

I do not know if what you ask is possible, but I suspect that it is not. On the other hand, I think you could do your work with an example by binding the property of the IsAlive object directly to the dependence property "IsMouseOver" using the parameter Mode = OneWayToSource.

+1
source

You might want to use an EventSetter , then do some code tuning using the DataContext property of the sender or GetBindingExpression .
This gives you the ability to set the handler at the style level.

+1
source

All Articles