我有这样的viewmodel:
public class ViewModel
{
public IView View { get; set; }
}
实现IView
的窗口。
我需要将此确切的Window绑定到view
属性,而不更改ViewModel
类。
这可能仅与该窗口的XAML有关吗?
我可以这样:https://stackoverflow.com/a/47266732/3206223
但是必须更改ViewModel
,在这种情况下,这是不可取的。
答案 0 :(得分:2)
您需要在XAML中实例化ViewModel并将其设置为DataContext:
<Window x:Class="MyApp.AppWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp.ViewModels">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
</Window>
编辑:
更改
window.DataContext = new ViewModel(properties);
window.ShowDialog();
到
var vm = new ViewModel(properties);
vm.View = window;
window.ShowDialog();