I am currently writing a user control with the MVVM pattern which has some Properties, e.g. Document.
DependencyProperty in the ViewModel
public static readonly DependencyProperty DocumentProperty = DependencyProperty.Register("Document", typeof(MyDocument), typeof(ResultControlViewModel), new PropertyMetadata(OnDocumentChanged));
public MyDocument Document
{
get { return (MyDocument)GetValue(DocumentProperty); }
set { SetValue(DocumentProperty, value); }
}
MainView which use the User Control
<control:ResultControl x:Name="myControl" />
How can I use my property “Document” from the ViewModel to bind them in XAML against the selected item of a ListBox in the MainView for example?
Programmaticlly. I can write a method in the code-behind of my user control, but this is I think not the beautiful way to do that. Especially with regard to the use of MVVM pattern.
Assuming that MainViewModel class have Documents and Document (i.e. current document) properties, the XAML should look like: