Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ICommand Dependency Property

I have an UserControl with a button inside. This button needs to add some items to a Grid that's inside said UC. I'm aware I can do this with a Click event.

The issue here is I am using MVVM and altering data outside their corresponding ViewModel would break the format (So to say).

Is there a way to create an ICommand Dependency Property so I can bind said DP to the button and have the functionality of adding the item to the Grid in my ViewModel? (I already have the List in both my UC and my ViewModel and they are working as expected)

Thank you.

like image 430
Xanagandr Avatar asked Feb 21 '15 19:02

Xanagandr


People also ask

What is a dependency property?

A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.

How do you use dependency property?

Custom Dependency Properties Follow the steps given below to define custom dependency property in C#. Declare and register your dependency property with system call register. Provide the setter and getter for the property. Define an instance handler which will handle any changes that occur to that particular instance.

When should I use dependency properties in WPF?

Dependency properties are used when you want data binding in a UserControl , and is the standard method of data binding for the WPF Framework controls. DPs have slightly better binding performance, and everything is provided to you when inside a UserControl to implement them.


1 Answers

Found a way to solve it in the way I was trying to. Leaving the answer here so people may use it:

1) In your User Control's code-behind, create a Dependency Property. I choose ICommand, since in my ViewModel I set it as a DelegateCommmand:

public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register(
        "Command",
        typeof(ICommand),
        typeof(UserControl));

    public ICommand Command
    {
        get 
        { 
            return (ICommand)GetValue(CommandProperty); 
        }

        set 
        { 
            SetValue(CommandProperty, value); 
        }
    }

2) In your UserControl's XAML code, bind this Dependency Property (In this case, a button):

<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">

    <Button Command="{Binding Command}" />

</Grid>

3) Next, on your ViewModel, declare a Command property and configure accordingly:

public ICommand ViewModelCommand { get; set; }

public ViewModelConstructor()
{
    ViewModelCommand = new DelegateCommand(ViewModelCommandExecute);
}

private void ViewModelCommandExecute()
{
    // Do something
}

4) Finally, on your View where the UserControl is nested, we declare the binding:

<UserControls:UserControl Command={Binding ViewModelCommand}/>

This way, the binding will take place and you can bind Commands from the buttons of any User Control to your ViewModels without breaking MVVM.

like image 198
Xanagandr Avatar answered Sep 20 '22 17:09

Xanagandr