From 554f93c086f03a11d828f03ff85de7a5d283cc4c Mon Sep 17 00:00:00 2001 From: MalzSmith <36475590+MalzSmith@users.noreply.github.com> Date: Wed, 8 Nov 2023 11:01:27 +0100 Subject: [PATCH 1/3] Assign VM as DataContext to created views --- templates/csharp/app-mvvm/ViewLocator.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/templates/csharp/app-mvvm/ViewLocator.cs b/templates/csharp/app-mvvm/ViewLocator.cs index df0cf646..bfad21eb 100644 --- a/templates/csharp/app-mvvm/ViewLocator.cs +++ b/templates/csharp/app-mvvm/ViewLocator.cs @@ -21,7 +21,9 @@ public IControl Build(object data) if (type != null) { - return (Control)Activator.CreateInstance(type)!; + var control = (Control)Activator.CreateInstance(type)!; + control.DataContext = data; + return control; } return new TextBlock { Text = "Not Found: " + name }; @@ -31,4 +33,4 @@ public bool Match(object? data) { return data is ViewModelBase; } -} \ No newline at end of file +} From 87b6845b1c0eb3fd8be9ac3a533208d34a967a84 Mon Sep 17 00:00:00 2001 From: dani Date: Wed, 8 Nov 2023 13:37:00 +0100 Subject: [PATCH 2/3] Assign VM to the created View as DataContext --- templates/fsharp/app-mvvm/ViewLocator.fs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templates/fsharp/app-mvvm/ViewLocator.fs b/templates/fsharp/app-mvvm/ViewLocator.fs index 654cba0c..c91e87fe 100644 --- a/templates/fsharp/app-mvvm/ViewLocator.fs +++ b/templates/fsharp/app-mvvm/ViewLocator.fs @@ -17,6 +17,8 @@ type ViewLocator() = if isNull typ then upcast TextBlock(Text = sprintf "Not Found: %s" name) else - downcast Activator.CreateInstance(typ) + let view = Activator.CreateInstance(typ) :?> IControl + view.DataContext <- data + view member this.Match(data) = data :? ViewModelBase From 77a1a53c4b319756549c1e7be8c28a8f2ba89698 Mon Sep 17 00:00:00 2001 From: MalzSmith <36475590+MalzSmith@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:18:04 +0100 Subject: [PATCH 3/3] Cast to Control instead of IControl --- templates/fsharp/app-mvvm/ViewLocator.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/fsharp/app-mvvm/ViewLocator.fs b/templates/fsharp/app-mvvm/ViewLocator.fs index c91e87fe..339c8506 100644 --- a/templates/fsharp/app-mvvm/ViewLocator.fs +++ b/templates/fsharp/app-mvvm/ViewLocator.fs @@ -17,7 +17,7 @@ type ViewLocator() = if isNull typ then upcast TextBlock(Text = sprintf "Not Found: %s" name) else - let view = Activator.CreateInstance(typ) :?> IControl + let view = Activator.CreateInstance(typ) :?> Control view.DataContext <- data view