简体   繁体   English

绑定控件的文本字段值时,在VS 2015设计器上可见的文本

[英]Text visible on VS 2015 designer when control's text field value is binded

Whenever I bind text property to view model property 每当我将文本属性绑定到视图模型属性时

<TextBlock Text="{Binding SomeExampleText}"/>

on the designer I see nothing in the place where my text will appear in runtime. 在设计器上,我看不到我的文本在运行时出现的位置。 When I use x:Bind: 当我使用x:Bind时:

<TextBlock Text="{x:Bind ViewModel.SomeExampleText}"/>

on the designer I see "ViewModel.SomeExampleText", sometimes it does not display full length because of lacking space (if the binding path is too long). 在设计器上,我看到“ ViewModel.SomeExampleText”,有时由于缺少空间(如果绑定路径太长)而无法显示完整长度。

Is there any way to have custom text displayed in designer just for preview instead of binding path or nothing as ashown above? 有什么方法可以在设计器中显示自定义文本,仅用于预览,而不是绑定路径或如上所示?

There are ways to create view models specifically for design time. 有多种方法可以专门为设计时间创建视图模型。 The simplest approach is probably this: 最简单的方法可能是这样的:

<TextBlock Text="{x:Bind ViewModel.SomeExampleText, FallbackValue='Hello!'}"/>

That one shows the string "Hello" in the designer, both with Binding and x:Bind . 那个在设计器中显示了字符串“ Hello”,都带有Bindingx:Bind

For Binding you can set the design-time data context something like this: 对于Binding您可以设置设计时数据上下文,如下所示:

<Page
    ...
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:viewModels="using:MyNameSpace.ViewModels"
    d:DataContext="{d:DesignInstance Type=viewModels:DesignTimeViewModel, IsDesignTimeCreatable=True}"
    mc:Ignorable="d">

The DesignTimeViewModel doesn't need any spcecific relation to your run-time view model; DesignTimeViewModel不需要与运行时视图模型有任何特定关系; it only needs to have suitable properties with the same names. 它只需要具有相同名称的合适属性即可。 If you are binding to collections, this is probably your best bet. 如果您要绑定收藏,这可能是您最好的选择。

Create a specific view model for the Design view in order to dial in the view's layout and style with realistic data that won't enter the run-time environment. 为“设计”视图创建一个特定的视图模型,以便使用不会进入运行时环境的实际数据来调入视图的布局和样式。

Since x:Bind looks to the code-behind for a strongly typed data source, you'll need to mimic that data binding path in the Design view. 由于x:Bind会在背后寻找强类型数据源的代码,因此您需要在“设计”视图中模拟该数据绑定路径。 Here's one way: 这是一种方法:

Apply the d:DataContext attribute to your view set the Type property to your view. 将d:DataContext属性应用于视图,将Type属性设置为视图。 When "IsDesignTimeCreatable" is true, it'll create a new instance of your code-behind. 当“ IsDesignTimeCreatable”为true时,它将创建代码隐藏的新实例。

d:DataContext="{d:DesignInstance Type=local:MainPage,IsDesignTimeCreatable=True}" 

Your code-behind likely has a ViewModel property that can be set to a design-time state with fake data or a run-time state with real data. 您的后台代码可能具有ViewModel属性,该属性可以设置为带有伪数据的设计时状态或带有真实数据的运行时状态。

This blog post shows an example: http://fast417.blogspot.com/2016/06/uwp-design-preview-with-xbind.html 该博客文章显示了一个示例: http : //fast417.blogspot.com/2016/06/uwp-design-preview-with-xbind.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定后将文本添加到TextBlock Text属性 - Add text to TextBlock Text property when it's binded 使Label控件文本看起来与VS表单设计器中的一样好 - make Label control text look as good as it does in VS form designer UWP:C# 中 RichEditBox 控件中的文本不可见(VS 2017) - UWP: Text not visible in RichEditBox control in C# (VS 2017) XAML设计器无法识别其他控件属性(VS 2015) - XAML designer can't recognize other control properties (VS 2015) WP7 - 自定义控件中的数据绑定值,如何设置默认字符串以便在设计器中可见 - WP7 - Data bound value in custom control, how to set default string so it's visible in designer 将参数传递给自定义控件的主函数时,VS 2010 Designer错误 - VS 2010 Designer error when passing arguments to custom's control main function VS Community 2015中的报表设计器 - Report Designer in VS Community 2015 如果控件位于不可见的 tabPage 上,我可以更新控件文本值吗 - Can I update a control text value if the control is on a tabPage that isn't visible 中继器外部控件的绑定值 - binded value for a control outside a repeater 如何获得用户控件的重写Text属性以显示在VS2005中的表单设计器中? - How can I get an overridden Text property of my user control to show up in the form designer in VS2005?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM