Cole W
Cole W

Reputation: 15303

Sharing non control related data between WPF ViewModel and UserControl

I'm new to WPF and the MVVM pattern so keep that in mind.

The project I'm tasked with working on has a view and a view model. The view also contains a user control that does NOT have a view model. There is data (custom object ... Order) being passed to the view model that I also need to share with the user control.

It looks like the UserControl does share data between the view model already via DependencyPropertys but this data is just text boxes on the user control that look to be bound back to propertys on the view model.

I need to share data that will NOT be represented by a control on the user control. Is there a good way to pass this data (complex Order object)? Maybe I do need some kind of hidden control on my user control to accomplish this but I'm just not that sure being new to this. Any advice would be appreciated.

Upvotes: 0

Views: 843

Answers (2)

ForbesLindesay
ForbesLindesay

Reputation: 10712

There is no need for hidden fields (or any such concept in WPF) as you can add any custom properties you want to a user control.

In the user control, create a new dependancy property like this but with MyUserControl set apropriately:

    public Order CurrentOrder
    {
        get { return (Order)GetValue(CurrentOrderProperty); }
        set { SetValue(CurrentOrderProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CurrentOrder.  This enables binding, etc.
    public static readonly DependencyProperty CurrentOrderProperty =
        DependencyProperty.Register("CurrentOrder", typeof(Order), typeof(MyUserControl), new PropertyMetadata(null, OnCurrentOrderPropertyChanged));

    public static void OnCurrentOrderPropertyChanged(DependencyObject Sender, DependencyPropertyChangedEventArgs e)
    {
        var sender = Sender as MyUserControl;
        var NewValue = e.NewValue as Order;
        var OldValue = e.OldValue as Order;
        if (OldValue != null && sender != null)
        {
            //Use old value as needed and use sender instead of this as method is static.
        }
        if (NewValue != null && sender != null)
        {
            //Use new value as needed and use sender instead of this as method is static.
        }
    }

In you're parent view where you use the usercontrol you then write something like:

<local:MyUserControl CurrentOrder="{Binding ViewModelOrder}" />

Where CurrentOrder is the dependancy property on the usercontrol and ViewModelOrder is the name of the property in the view model you would need to replace local:MyUserControl with your control name/namespace.

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

You can simply create a dependency property in the class of your UserControl and bind to it in the View that uses the control. There is no need to internally bind the dependency property to one of the controls in the UserControl.

Upvotes: 1

Related Questions