Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Sometimes a view (for example, a user control) contains additional properties besides the DataContext to interact with the view model. By default, it is hard to implement this in an MVVM sccenario, but Catel solves this issue using the ViewToViewModel attribute.

...

Code Block
public partial class MyControl : UserControl
{
    static MyControl()
    {
        typeof(MyControl).AutoDetectViewPropertiesToSubscribe();
    }
	public MyControl()
	{
		InitializeComponent();
	}
 
	[ViewToViewModel(MappingType = ViewToViewModelMappingType.ViewModelToView)]
	public GeoCoordinate MapCenter
	{
		get { return (GeoCoordinate) GetValue(MapCenterProperty); }
		set { SetValue(MapCenterProperty, value); }
	}
 
	// Using a DependencyProperty as the backing store for MapCenter.  This enables animation, styling, binding, etc...
	public static readonly DependencyProperty MapCenterProperty = DependencyProperty.Register("MapCenter", typeof (GeoCoordinate),
		typeof (MyControl), new PropertyMetadata(null, (sender, e) => ((MyControl) sender).UpdateMapCenter()));
 
	private void UpdateMapCenter()
	{
		map.SetView(ViewModel.MapCenter, ViewModel.ZoomLevel);
	}
 
	public new MainPageViewModel ViewModel
	{
		get { return base.ViewModel as MainPageViewModel; }
	}
}

...