Skip to content

Commit

Permalink
Merge pull request #238 from MalzSmith/master
Browse files Browse the repository at this point in the history
Assign ViewModels as DataContext to created Views in MVVM templates
  • Loading branch information
Takoooooo authored Nov 13, 2023
2 parents 8d27f8a + 77a1a53 commit 66f3823
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions templates/csharp/app-mvvm/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -31,4 +33,4 @@ public bool Match(object? data)
{
return data is ViewModelBase;
}
}
}
4 changes: 3 additions & 1 deletion templates/fsharp/app-mvvm/ViewLocator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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) :?> Control
view.DataContext <- data
view

member this.Match(data) = data :? ViewModelBase

0 comments on commit 66f3823

Please sign in to comment.