-1

I have two DependencyProperties TBLocation & TBBroadcastLocation in MyUserControl. For TBLocation I get information from another element called MyTextBox. When TBLocation gets set, I set TBBroadcastLocation with TBLocation's Point information. And now I would like to make TBBroadcastLocation data available to MyViewModel so that it has TBLocation's data indirectly available to it.

How could I do that or do we have a better approach?

John
  • 693
  • 1
  • 12
  • 37
  • 1
    What does 'For DP2 I would like to pass it to another ViewModel' mean? Please think carefully before you ask questions... have you provided enough information for people to help? Is your question clear? Will people from other countries understand what you're after? – Sheridan Sep 17 '13 at 09:30
  • you want to set two different properties from two different view models? – Omri Btian Sep 17 '13 at 09:30
  • Can you illustrate your question with some code? though your question seems a bit unfeasible. http://stackoverflow.com/questions/how-to-ask – HichemSeeSharp Sep 17 '13 at 09:30
  • I'm still not sure that I understand... are you simply asking 'How can I bind a property of a `UserControl` to a property of a view model', because there are plenty of examples of that all over the internet... if that's *not* what you're after, you *may* need to be more specific. – Sheridan Sep 17 '13 at 09:47
  • Could you have a look at the new description? Thank you. – John Sep 17 '13 at 10:09
  • Just so that you know... when I asked for more information, I was *not* asking to see names... names are irrelevant. The 'types' of your properties is more important. Some XAML to give us some context is more important... etc. – Sheridan Sep 17 '13 at 10:28

1 Answers1

1

It seems to me as if you are asking 'How can I bind a property of a UserControl to a property of a view model'. You really should read the basics of data binding before asking these questions here. For future reference, please read the Data Binding Overview page at MSDN.

Given that you still have not provided enough information, I will assume that your property is of type string. In this case, your view model will need a standard property of type string to bind to your DependencyProperty... this property must implement the INotifyPropertyChanged interface:

private string viewModelProperty = string.Empty;

public string ViewModelProperty
{
    get { return viewModelProperty; }
    set { viewModelProperty = value; NotifyPropertyChanged("ViewModelProperty"); } }
}

Make sure that the DataContext of the Window that has your UserControl in it is set to an instance of the view model class:

In the MainWindow constructor:

DataContext = new ViewModelClass();

Or in XAML:

<DataTemplate DataType="{x:Type ViewModels:ViewModelClass}">
    <Views:yourView />
</DataTemplate>

Then you simply bind with a Two Way binding:

<YourNamespace:MyUserControl 
    TBBroadcastLocation="{Binding ViewModelProperty, Mode=TwoWay}" />
Sheridan
  • 68,826
  • 24
  • 143
  • 183