Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 9850b9e

Browse files
committed
Make DataContext a part of the IView
We need to set the ViewModel via DataContext, otherwise WPF won't trigger binding events, so make DataContext a part of the IView, and make sure ViewModel is not readonly. Also, make ViewModel typed, no reason for it to be object.
1 parent 3d7a8d0 commit 9850b9e

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

src/GitHub.App/Controllers/UIController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,13 +688,13 @@ bool CreateViewAndViewModel(UIViewType viewType)
688688
{
689689
var d = list[viewType];
690690
if (d.View.ViewModel == null)
691-
d.View.ViewModel = d.ViewModel;
691+
d.View.DataContext = d.ViewModel;
692692
}
693693

694694
if (!list.ContainsKey(viewType))
695695
{
696696
var d = factory.CreateViewAndViewModel(viewType);
697-
d.View.ViewModel = d.ViewModel;
697+
d.View.DataContext = d.ViewModel;
698698
list.Add(viewType, d);
699699
return true;
700700
}

src/GitHub.App/Factories/UIFactory.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public UIFactory(IExportFactoryProvider factory)
3333
/// <returns>true if the View/ViewModel didn't exist and had to be created</returns>
3434
public IUIPair CreateViewAndViewModel(UIViewType viewType)
3535
{
36-
var d = new UIPair(viewType, factory.GetView(viewType), factory.GetViewModel(viewType));
37-
d.View.ViewModel = d.ViewModel;
38-
return d;
36+
return new UIPair(viewType, factory.GetView(viewType), factory.GetViewModel(viewType));
3937
}
4038
}
4139

src/GitHub.Exports/UI/IView.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
using System;
1+
using GitHub.ViewModels;
2+
using System;
23

34
namespace GitHub.UI
45
{
56
public interface IView
67
{
7-
object ViewModel { get; set; }
8+
IViewModel ViewModel { get; }
89
IObservable<object> Done { get; }
910
IObservable<object> Cancel { get; }
1011
IObservable<bool> IsBusy { get; }
12+
// necessary for WPF to trigger binding events
13+
object DataContext { get; set; }
14+
1115
}
1216

1317
public interface IHasDetailView

src/GitHub.UI.Reactive/Controls/SimpleViewUserControl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public void Dispose()
8989
}
9090

9191
public class SimpleViewUserControl<TViewModel, TImplementor> : SimpleViewUserControl, IViewFor<TViewModel>, IView
92-
where TViewModel : class, IViewModel where TImplementor : class
92+
where TViewModel : class, IViewModel
93+
where TImplementor : class
9394
{
9495
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
9596
"ViewModel", typeof(TViewModel), typeof(TImplementor), new PropertyMetadata(null));
@@ -103,11 +104,10 @@ object IViewFor.ViewModel
103104
}
104105

105106
[AllowNull]
106-
object IView.ViewModel
107+
IViewModel IView.ViewModel
107108
{
108109
[return: AllowNull]
109110
get { return ViewModel; }
110-
set { ViewModel = (TViewModel)value; }
111111
}
112112

113113
[AllowNull]

src/GitHub.VisualStudio/UI/GitHubPane.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ IView View
3939
get { return Content as IView; }
4040
set { Content = value; }
4141
}
42-
IViewModel ViewModel
43-
{
44-
get { return (Content as UserControl).DataContext as IViewModel; }
45-
set { (Content as UserControl).DataContext = value; }
46-
}
4742

4843
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
4944
public GitHubPane() : base(null)
@@ -64,20 +59,20 @@ public GitHubPane() : base(null)
6459
var d = factory.CreateViewAndViewModel(Exports.UIViewType.GitHubPane);
6560
// placeholder logic to load the view until the UIController is able to do it for us
6661
View = d.View;
67-
ViewModel = d.ViewModel;
62+
View.DataContext = d.ViewModel;
6863
}
6964

7065
protected override void Initialize()
7166
{
7267
base.Initialize();
73-
var vm = (Content as IView).ViewModel as IServiceProviderAware;
68+
var vm = View.ViewModel as IServiceProviderAware;
7469
Debug.Assert(vm != null);
7570
vm?.Initialize(this);
7671
}
7772

7873
public void ShowView(ViewWithData data)
7974
{
80-
ViewModel?.Initialize(data);
75+
View.ViewModel?.Initialize(data);
8176
}
8277

8378
[return: AllowNull]

0 commit comments

Comments
 (0)