views:

127

answers:

2

I have defined a default binding in my style.

For example I have configured the visibility binding of my button so that it must not be visible if the relative command can not execute.

This is my default binding behavior.

Apart from my default behavior, every view can customize the buttons it uses with another visibility binding.

I want to combine the two bindings so that if any of two say "it's not visible" it will be not visible!

In other words, is it possibile to create a binding behavior hierarchy? Thanks!

+3  A: 

use a MultiBinding to wire up all the different bindings in XAML, and write your own IMultiValueConverter that prioritises / ands / ors each value as you like. You can't use a MultiBinding without an IMultiValueConverter (or a StringFormat, but that's no use to you)

Note that PriorityBinding is NOT what you are looking for here.

here's a valueConverter you can use:

[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleansAndToVisibilityMultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        Func<bool, bool, bool> aggregator = (x, y) => x && y;
        bool aggregate = values.Cast<bool>().Aggregate(aggregator);
        return aggregate ? Visibility.Visible : Visibility.Collapsed;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
Rob Fonseca-Ensor
it's a good solution but I have two bindings collapsing in the same property: a "visibility" setted in theme and a "visibility" setted in xaml
Ricibald
+1  A: 

No, sorry, not possible in XAML.

You CAN do this combination in code, though. You can write a BindingConverter for this.

TomTom