如何为绑定到 viewmodel 属性的 silverlight 文本块提供设计时值?
我的 XAML 中有一个 TextBlock,其文本绑定到我的视图模型中的属性。
<TextBlock x:Name="SomeText" Text="{Binding TheTextProperty}" />
这工作正常,但在设计时,没有视图模型,因此该属性无法解析并且文本为空白。这在设计器中很难使用,因为它不显示可见的文本。
如何指定设计时使用的一些默认文本?
I have a TextBlock in my XAML that has its text bound to a property in my viewmodel.
<TextBlock x:Name="SomeText" Text="{Binding TheTextProperty}" />
This works fine, but at design time, there is no viewmodel so the property is unresolvable and the text is blank. This is hard to work with in the designer because it shows no visible text.
How can I specify some default text to use at design time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许你可以尝试
Maybe you could try
as documented here.
您还可以使用 设计时数据在您的解决方案中提供丰富的绑定体验。设置和运行有点困难,但要点如下。
首先,在 xaml 中创建 DataContext。将新的 Xml 文档添加到您的解决方案(根目录是一个好地方)并为其指定 .xaml 扩展名。在本示例中,我们将此文件称为“foo.xaml”。
在此文件中,删除所有 XML 并开始创建 DataContext 类型的实例。例如,如果您的 DataContext 是一个字符串(非常简单的示例),您的文件将如下所示:
您的文件可能如下所示
将此文件上的构建操作设置为 DesignDataWithDesignTimeCreatableTypes:
接下来,在视图中,将以下命名空间和属性添加到 Window/UserControl 的根目录中:
Source
相对于当前文档。所以,如果你的解决方案看起来像您可以将
Source
设置为../Data/foo.xaml
。还有其他方法可以创建 DesignData 上下文,具体取决于您的 ViewModel 是否可以在设计时实例化等。
You can also use Design Time Data to provide a rich binding experience in your solution. Its a little hard to set up and get running, but here's the gist.
First, you create your DataContext in xaml. Add a new Xml document to your solution (the root is a good place) and give it an .xaml extension. Lets call this file "foo.xaml" for this example.
In this file, remove all of the XML and start creating an instance of your DataContext type. For example, if your DataContext was a string (very simple example) your file would look like the following:
You might have yours look like
Set the Build Action on this file to DesignDataWithDesignTimeCreatableTypes:
Next, in your View, add the following namespaces and properties to the root of your Window/UserControl:
Source
is relative to the current document. So, if your solution looked likeyou would set the
Source
to../Data/foo.xaml
.There are other ways to create a DesignData context depending on whether your ViewModel can be instantiated at design time, etc.
另一种选择是使用 DesignerProperties.GetIsInDesignMode函数来确定控件是否托管在 VS/Blend 中,并在这种情况下生成假 DataContext。 Laurent Bugnion 在此中提供了许多有关如何创建和使用设计时数据的示例帖子
Another option is to use the DesignerProperties.GetIsInDesignMode function to determine if the control is hosted in VS/Blend and Generate a fake DataContext in that case. Laurent Bugnion provides a number of examples of how to create and use design-time data in this post