25

Assuming a code like below,

public class SomeViewModel{
      ICommand ReloadCommand{get...}
      ICommand SaveCommand{get..}
}

//SomeView.xaml
<SomeCustomControl Reload="ReloadCommand" Save="SaveCommand" /> //NOT SURE HOW??

//SomeCustomContro.xaml
<SomeCustomControl x:Name="someCustomControl">
<Button Command={Binding ElementName=someCustomControl, Path=Reload />
<Button Command={Binding ElementName=someCustomControl, Path=Save />
</SomeCustomControl>

//SomeCustomControl.xaml.cs
.....  //NOT SURE HOW TO ALLOW BINDING TO A ICOMMAND ??

In my SomeCustomControl, I need to support "binding of ICommand in xaml". I understand DependencyProperties could be bind like this, but in this case I need to bind ICommand.

EDIT
I can use the DataContext SomeView in SomeCustomControl. There is more logic and separation between the two which I can not dissolve. I 'must' maintain a reference of Reload/Save ICommands somewhere in my SomeCustomControl.

2 Answers 2

50

Let me get you straight, you want to bind to the Reload and Save right?

So that needs creating, declaring and defining two dependency properties ReloadCommandProperty and SaveCommandProperty of type ICommand for SomeCustomControl.

So assuming that SomeCustomControl derives from Control ...

public class SomeCustomControl : Control
{
    public static DependencyProperty ReloadCommandProperty
        = DependencyProperty.Register(
            "ReloadCommand",
            typeof (ICommand),
            typeof (SomeCustomControl));

    public static DependencyProperty SaveCommandProperty
        = DependencyProperty.Register(
            "SaveCommand",
            typeof(ICommand),
            typeof(SomeCustomControl));

    public ICommand ReloadCommand
    {
        get
        {
            return (ICommand)GetValue(ReloadCommandProperty);
        }

        set
        {
            SetValue(ReloadCommandProperty, value);
        }
    }

    public ICommand SaveCommand
    {
        get
        {
            return (ICommand)GetValue(SaveCommandProperty);
        }

        set
        {
            SetValue(SaveCommandProperty, value);
        }
    }
}

After this proper binding to RelodCommand and SaveCommand properties will start working...

     <SomeCustomControl RelodCommand="{Binding ViewModelReloadCommand}"
                        SaveCommand="{Binding ViewModelSaveCommand}" /> 
Sign up to request clarification or add additional context in comments.

Comments

3

Create a property that will return your command and bind this property wherever needed.

private ICommand _reloadCommand;
public ICommand ReloadCommand
{
  get 
  { 
    if(_reloadCommand == null) _reloadCommand = CreateReloadCommand();
    return _reloadCommand;
  }
}

Change the binding in your code to

<Button Command={Binding ReloadCommand}" />

And bind the custom control DataContext to the view model that contains the commands.

2 Comments

Do you mean I can directly bind a ICommand instance to a property in a control?? Also, what is "CreateReloadCommand" for? This command instance will be created in ViewModel, and SomeCustomControl just needs to store that instance reference (using xaml binding).
You can bind value of ICommand type to whatever you want, the most meaningful dependency property for binding is ButtonBase.CommandProperty. CreateReloadCommand is a method creating your command - I did not wanted to write the command creation and initialization code. When I was typing my answer, you have methods for commands in your code (you've changed them later).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.