Files
TheXamlGuy.TaskbarGroup/TheXamlGuy.TaskbarGroup.Flyout.Foundation/IBindViewModelExtensions.cs
T
dan_clark@outlook.com 2ac0e3ed26 project
2022-03-23 15:44:32 +00:00

38 lines
1.3 KiB
C#

using TheXamlGuy.TaskbarGroup.Core;
using Windows.UI.Xaml;
namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
{
public static class IBindViewModelExtensions
{
public static void Bind<TViewModel>(this IBindViewModel<TViewModel> view) where TViewModel : class
{
if (view is FrameworkElement frameworkElement)
{
void DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
var viewModelProperty = view.GetType().GetProperty("ViewModel");
if (viewModelProperty is not null)
{
viewModelProperty.SetMethod.Invoke(sender, new[] { sender.DataContext });
}
}
frameworkElement.DataContextChanged += DataContextChanged;
}
}
public static void Bind<TViewModel>(this IBindViewModel<TViewModel> view, object viewModel) where TViewModel : class
{
if (view is FrameworkElement frameworkElement)
{
var viewModelProperty = view.GetType().GetProperty("ViewModel");
if (viewModelProperty is not null)
{
viewModelProperty.SetValue(frameworkElement, viewModel);
}
}
}
}
}