如何在 XAML 中设置 d:DesignInstance 的属性? [英] How to set properties of a d:DesignInstance in XAML?
问题描述
我正在使用 4.0 系列 WPF 工具的新 d:DesignInstance 功能.效果很好!
I'm using the new d:DesignInstance feature of the 4.0 series WPF tools. Works great!
我遇到的唯一问题是:如何在实例上设置属性?鉴于这样的事情:
Only issue I'm having is: how can I set properties on the instance? Given something like this:
<Grid d:DataContext="{d:DesignInstance plugin:SamplePendingChangesViewModel, IsDesignTimeCreatable=True}"/>
除了在默认构造函数中设置属性或通过其他对象初始值设定项进行路由之外,如何在视图模型上设置属性?
How can I set properties on the viewmodel, aside from setting them in its default ctor or routing it through some other object initializer?
我试了一下,但 VS 在编译时出现错误d:DataContext was not found":
I gave this a try but VS gives errors on compile "d:DataContext was not found":
<Grid>
<d:DataContext>
<d:DesignInstance IsDesignTimeCreatable="True">
<plugin:SamplePendingChangesViewModel ActiveTagIndex="2"/>
</d:DesignInstance>
</d:DataContext>
目前我将返回使用资源和d:DataContext={StaticResource SampleData}",我可以在其中设置资源中的属性.
For the moment I'm going back to using a resource and 'd:DataContext={StaticResource SampleData}', where I can set the properties in the resource.
有没有办法通过 d:DesignInstance 做到这一点?
Is there a way to do it via a d:DesignInstance?
推荐答案
作为@jberger,你应该使用 d:DesignData
而不是内联 d:DataContext
.
As @jberger you should probably use d:DesignData
instead of inlining a d:DataContext
.
然而,您也可以在 xaml 文件中设置 d:DataContext 内联,秘诀是使用正确的类 (DesignProperties
) 来限定 d:DataContext
属性:
However you can set the d:DataContext inline in the xaml file as well, the secret is to use the correct class (DesignProperties
) to qualify the d:DataContext
property:
<d:DesignProperties.DataContext>
<plugin:SamplePendingChangesViewModel ActiveTagIndex="2"/>
</d:DesignProperties.DataContext>
你怎么知道哪个班级有资格?将鼠标悬停在属性语法中设置的属性上,将出现带有完全限定属性名称的工具提示.
How do you know what class to qualify with? Mouse over a property that is set in attribute syntax and a tooltip will appear with the fully qualified property name.
还要注意,我没有使用 d:DesignInstance
标记扩展,因为它的具体工作是创建一个您为其提供名称的类型的实例(或生成该类型的代理,如果它不能在设计时实例化).那不是我们想要的,在这种情况下,我们想在内联 xaml 中定义实例.
Note also that im not using the d:DesignInstance
markup exstension as its job specifically is to create a instance of a type that you provide the name for (or generate a proxy of that type if it cant be instanciated at design-time). Thats not what we want, we want to define the instance in inline xaml in this case.
确实,d:DesignData
(也是一个标记扩展)的工作方式大致相同,除了它查找一个 xaml 文件并将其反序列化为要使用的实际实例,而不是仅使用默认值构造函数.
Indeed, d:DesignData
(also a markup extension) works much the same way, except that it looks for a xaml file and deserializes that to the actual instance to use instead of just using the default constructor.
为了完整起见,我还应该提到您也可以通过使用它们的完整类名 (xxxExtension) 将 DesignData 和 DesignInstance 与元素语法一起使用:
Just for completeness i should also mention that you can use DesignData and DesignInstance with element syntax as well by using their full class names (xxxExtension):
<d:DesignProperties.DataContext>
<d:DesignDataExtension Source="SampleData.xaml"></d:DesignDataExtension>
</d:DesignProperties.DataContext>
这适用于大多数标记扩展,但不需要遵循此命名约定(绑定类是一个明显的例外)可以在此处找到更多信息:
This is true for most markup exstensions but its not required to follow this naming convension (The Binding class is a notable exception) More info can be found here:
这篇关于如何在 XAML 中设置 d:DesignInstance 的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!