Framework-Class-Library-Extension icon indicating copy to clipboard operation
Framework-Class-Library-Extension copied to clipboard

Condition converter does not work with the control template and Template binding.

Open dominkomin opened this issue 10 years ago • 3 comments
trafficstars

When I try to use the Condition Converter (or Single Condition Converter) inside of a Control Template with a Template binding following exception is thrown: "Set property 'System.Windows.TemplateBindingExtension.Converter' threw an exception. Object of type 'Whathecode.System.Windows.Data.SingleConditionConverterExtension' cannot be converted to type 'System.Windows.Data.IValueConverter'."

Stack trace: System.Windows.Markup.XamlParseException occurred HResult=-2146233087 Message=Set property 'System.Windows.TemplateBindingExtension.Converter' threw an exception. Source=PresentationFramework LineNumber=0 LinePosition=0 StackTrace: at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter) InnerException: System.ArgumentException HResult=-2147024809 Message=Object of type 'Whathecode.System.Windows.Data.SingleConditionConverterExtension' cannot be converted to type 'System.Windows.Data.IValueConverter'. Source=mscorlib StackTrace: at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Xaml.Schema.SafeReflectionInvoker.InvokeMethodCritical(MethodInfo method, Object instance, Object[] args) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value) InnerException:

dominkomin avatar Sep 24 '15 12:09 dominkomin

Could you add the XAML code where you use the converter? Also you can use markdown formatting so it is actually readable. ;p

Whathecode avatar Sep 28 '15 14:09 Whathecode

I don't know whether I have tried using that specific converter within a control template before, but the TimeLine control seems to at least use the EqualsConverter (which extends from AbstractMultiConverter<object, TTo> and follows a similar logic I believe) and works.

Whathecode avatar Sep 28 '15 14:09 Whathecode

That is the non-working XAML line

Visibility="{TemplateBinding NotificationsCount, Converter={data:SingleConditionConverter Expression='[0] > 0', IfFalse={x:Static Visibility.Hidden}, IfTrue={x:Static Visibility.Visible}}}"

I tried doing your trick (written below) and it seems to be working. Sounds to me like a weird XAML bug.

<Canvas.Visibility>
    <MultiBinding Converter="{data:ConditionConverter Expression='[0] > 0', IfTrue={x:Static Visibility.Visible}, IfFalse={x:Static Visibility.Collapsed}}">
        <Binding Path="NotificationsCount" RelativeSource="{RelativeSource TemplatedParent}" />
    </MultiBinding>
</Canvas.Visibility>

Another solution is to create a new stand-alone converter, it is working as well:

class NumberToVisibilityConverter : IValueConverter
{
    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
        return (int)value > 0 ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
        return value;
    }
}

dominkomin avatar Sep 28 '15 15:09 dominkomin