Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在XAML中为根元素设置DataContext而不使用property元素语法_.net_Wpf_Xaml_Datacontext - Fatal编程技术网

.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}"