Francesco
Francesco

Reputation: 1848

Silverlight: bind command of a generic component

I have a problem.

I created a generic container that I use in my application. This container exports two properties (for now): its content and a button action.

The problem is that I can't bind the action.

The generic container has a button:

<HyperlinkButton x:Name="expandButton" Width="16">

In the code behind I export a ICommand property:

public ICommand ExpandCommand 
        {
            get { return Click.GetCommand(expandButton); }
            set { Click.SetCommand(expandButton, value); }
        }

And, when I trie to use it:

<abo:DashBoardPanel Margin="10" Grid.Row="1" Title="Command Panel"
                                    DataContext="{Binding Commands}"
                                    ExpandCommand="{Binding test}"
                                    >

When the component (the one which uses the generic container) is allocated the following exception fires:

{System.ArgumentException: Un oggetto di tipo 'System.Windows.Data.Binding' non può essere convertito nel tipo 'System.Windows.Input.ICommand'. su System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) su System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) su System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) su System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) su System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
su System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) su System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) su MS.Internal.XamlMemberInfo.SetValue(Object target, Object value) su MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)}

(It's italian.. the translation is something like "An object of type 'System.Windows.Data.Binding' cannot be converted into 'System.Windows.Input.ICommand')

Any ideas?

Thank you. Francesco

Upvotes: 0

Views: 238

Answers (1)

bperreault
bperreault

Reputation: 994

I think ExpandCommand needs to be a dependency property to be able to bind to it. At least from what you've shown, that is the first place I would start editing.

ExpandCommand would look like this in your generic class:

      public static readonly DependencyProperty ExpandCommandProperty =
       DependencyProperty.Register(
       "ExpandCommand",
       typeof(ICommand),
       typeof(genericobject),
       new PropertyMetadata(OnExpandCommandChanged));

    public ICommand ExpandCommand
    {
        get
        {
            return (ICommand)this.GetValue(ExpandCommandProperty);
        }
        set
        {
            this.SetValue(ExpandCommandProperty, value);
        }
    }
    private static void OnExpandCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        genericobject picker = (genericobject)d;
        if (picker == null)
            return;

    }

Upvotes: 2

Related Questions