// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using Avalonia; using Avalonia.VisualTree; using Avalonia.Controls; using ReactiveUI; namespace Avalonia.ReactiveUI { /// /// A ReactiveUI UserControl that implements /// and will activate your ViewModel automatically if it supports activation. /// /// ViewModel type. public class ReactiveUserControl : UserControl, IViewFor where TViewModel : class { public static readonly AvaloniaProperty ViewModelProperty = AvaloniaProperty .Register, TViewModel>(nameof(ViewModel)); /// /// Initializes a new instance of the class. /// public ReactiveUserControl() { DataContextChanged += (sender, args) => ViewModel = DataContext as TViewModel; } /// /// The ViewModel. /// public TViewModel ViewModel { get => GetValue(ViewModelProperty); set => SetValue(ViewModelProperty, value); } object IViewFor.ViewModel { get => ViewModel; set => ViewModel = (TViewModel)value; } } }