I want to compare two dynamic values User_id
and user_id
for equality and setting one property Cursor
. Also, when the cursor is hand, I have to execute one function. How to do it? This is the code that I am using:
<DataTrigger Binding="{Binding Path=User_id}" Value="{Binding Path=user_id}">
<Setter Property="Cursor" Value="Hand"/>
</DataTrigger>
There are a couple options to attack this.
#1. Multibinding Converter
You can use
Multibinding
to input the two values into aIMultiValueConverter
. To use this type of binding in yourDataTrigger
, you would use follow the following syntax.The
MultiBinding.Converter
is set to a new instance ofEqualityConverter
, which is a class I created that implements theIMultiValueConverter
interface. This class will do the comparison for you. TheDataTrigger
triggers when this converter returns true.#2. MVVM Pattern
I'm not sure where your
DataContext
is coming from, but if possible, you may want to consider using a view model for your binding. The view model could expose a property that does the equality comparison for you. Something like this.If using a view model like this, your
DataTrigger
could simplify to..Regarding executing a function on the trigger, I added a
DoSomething
method to highlight how the view model could be used to execute a function when the two IDs are equal. I'm not sure if that would work for your case because I'm not sure what the intent of the function call is, but it is a way to execute a function when a condition changes.