.net 在XAML中为根元素设置DataContext而不使用property元素语法
我想知道是否可以这样写:.net 在XAML中为根元素设置DataContext而不使用property元素语法,.net,wpf,xaml,datacontext,.net,Wpf,Xaml,Datacontext,我想知道是否可以这样写: <Window ... xmlns definitions ... DataContext=<!--Create an instance here--> ></Window> <UserControl DataContext="{CreateNew local:CustomViewModel}" DataContext=“{x:静态SomeClass.SomeProperty}”如何,然后在SomeClas
<Window
... xmlns definitions ...
DataContext=<!--Create an instance here-->
></Window>
<UserControl DataContext="{CreateNew local:CustomViewModel}"
DataContext=“{x:静态SomeClass.SomeProperty}”如何,然后在SomeClass中:
public static object SomeProperty { get { return new object(); } }
其中,对象是要创建的类型。我不确定x:Static是否缓存了它曾经检索到的对象,但如果不是,这将起作用。你也可以试着起诉
它允许您调用方法、构造函数和属性。我能清楚地看到这一点的唯一方法是编写自己的使用(或DI容器)创建VM的方法,从而为您提供如下语法:
<Window
... xmlns definitions ...
DataContext=<!--Create an instance here-->
></Window>
<UserControl DataContext="{CreateNew local:CustomViewModel}"