Click here to Skip to main content
15,669,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dependency property on a control in my View that is bound to a field on my ViewModel. When the user clicks a menu item I want the control to update the value of that property so the ViewModel can save it in an XML file. What is the correct mechanism to have the ViewModel request that the View update that property?

Edit: move the code from comment below

   "open bracket" controls:MyControl 
   MyProperty="{Binding MyProperty, Mode=TwoWay}" "close bracket"
   MyControl class:
   public static readonly DependencyProperty MyPropertyProperty =
                  new PropertyMetadata(null)
   ViewModel property:
	byte [] MyProperty {get;set;}

// Code on ViewModel that is run when the MenuItem is clicked:
   public void MyMenuItemFunction()
      // Get the up-to-date value of MyProperty from MyControl here
Updated 26-Mar-12 13:26pm

1 solution

You should use command binding. The ViewModel would expose an ICommand property. You would derive a class from ICommand that will handle the Execute. When you instantiate the command object from your ViewModel, you can pass anything else you need into there.

The menu command can then use binding via its Command property. That will then call your Execute method when pressed.
Share this answer
bscaer 26-Mar-12 16:08pm    
Thanks. I do have a menu command bound to the ViewModel but when that command is executed how does the ViewModel tell the control to update the value of its property?
SteveAdey 26-Mar-12 16:10pm    
Which property? Is it one on your ViewModel?
bscaer 26-Mar-12 16:14pm    
I have a dependency property on a control in my View that is bound to a field on my ViewModel. I want the ViewModel to tell the View to update the value for the property.
SteveAdey 26-Mar-12 16:15pm    
Maybe I'm missing something, but why can't you just modify the property on your ViewModel directly, rather than get the control to do it?
bscaer 26-Mar-12 16:54pm    
When the menu item is clicked, I need to tell the control to calculate a value and pass it back to the ViewModel. How to tell the control to calculate the value at that point in time?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900