This commit is contained in:
TheXamlGuy
2024-02-15 20:59:36 +00:00
parent 6f5f6f8cfe
commit 41b9d5e0fc
5 changed files with 39 additions and 20 deletions
+12 -8
View File
@@ -3,27 +3,31 @@ using System.Reflection;
namespace Hyperbar.UI.Windows;
public class ViewModelContentBinder(NavigationTargetCollection contents) :
public class ViewModelContentBinder(NavigationTargetCollection contents) :
IViewModelContentBinder
{
public void Bind(FrameworkElement view,
object context)
{
if (context.GetType().GetCustomAttributes<NavigationTargetAttribute>()
is IEnumerable<NavigationTargetAttribute> attributes)
{
foreach (NavigationTargetAttribute attribute in attributes)
{
if (view.FindName(attribute.Name) is FrameworkElement content)
if (!contents.ContainsKey(attribute.Name))
{
contents.Add(attribute.Name, content);
void HandleUnloaded(object sender, RoutedEventArgs args)
if (view.FindName(attribute.Name) is FrameworkElement content)
{
view.Unloaded -= HandleUnloaded;
contents.Remove(attribute.Name);
}
contents.Add(attribute.Name, content);
void HandleUnloaded(object sender, RoutedEventArgs args)
{
view.Unloaded -= HandleUnloaded;
contents.Remove(attribute.Name);
}
view.Unloaded += HandleUnloaded;
view.Unloaded += HandleUnloaded;
}
}
}
}