views:

40

answers:

2

I want to activate a trigger if the bound items property ID is equal to a property in my custom control.

But i can't use bindings in my triggers!

How would i do this?

A: 

DataTrigger's value property doesn't take bindings?


    <DataTrigger Binding="{Binding BoundPropertyId}" Value="{Binding ElementName=Mine, Path=Property}" />

If that doesn't work, you might need to hook onto an event and process it yourself in the code-behind.

apandit
I can't use binding in the Value-tag
Erik
You can't set Binding on Value because it isn't a DependencyProperty. However, as @apandit said, DataTrigger is what you are looking for!
Mihir Gokani
+2  A: 

This should work:

<DataTrigger
    Value="True">

    <DataTrigger.Binding>
        <MultiBinding
            Converter="{x:Static local:EqualityConverter.Instance}">
            <Binding
                Path="BoundProperty" />
            <Binding
                ElementName="MockCustomControl"
                Path="Text" />
        </MultiBinding>
    </DataTrigger.Binding>

    <DataTrigger.Setters>
        <Setter
            TargetName=" ... "
            Property=" ... "
            Value=" ... " />

         ...
         ...

    </DataTrigger.Setters>

</DataTrigger>

And the converter may be something similar to this:

public class EqualityConverter : IMultiValueConverter
{
    public static readonly EqualityConverter Instance = new EqualityConverter();

    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values == null || values.Length == 0) return false;

        for (int i = 1; i < values.Length; i++)
            if (!values[i].Equals(values[0])) return false;

        return true;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Note that I've used a generic convrter that can campare n values, but you can use a simple one as per your needs.

Hope this helps.

Mihir Gokani
I Always get values[1] set to DependencyProperty.UnsetValue on the binding to the property. I have tried many variations but always get UnsetValue! Suggestions?
Erik
on my side, it's working perfectly! It seems that there's some issue with your bindings. Where's your control (which i've named `MockCustomControl`) situated in visual tree? May be you can upload your project somewhere (like http://SkyDrive.com) so that I can help you further :)
Mihir Gokani
Got it working!
Erik