如何在WPF Visual Studio Designer中显示占位符值,直到可以加载实际值为止 [英] How to display placeholder value in WPF Visual Studio Designer until real value can be loaded
问题描述
我是一位经验丰富的C#开发人员,但还是WPF新手.我无法通过网络搜索找到答案的基本问题(我认为).这是简化的用例...
I'm an experienced C# developer but a WPF newbie. Basic question (I think) that I can't find an answer to by web searching. Here's the simplified use case...
我想在WPF TextBlock中显示一个字符串.所以我在XAML控件的代码后面写了一些C#代码...
I want to display a string in a WPF TextBlock. So I write some C# code in codebehind of my XAML control...
public class MyCoolControl : UserControl
{
public void InitializeMyCoolControl()
{
this.DataContext = "SomeStringOnlyAvailableAtRuntime"; // Perhaps from a database or something...
}
}
我这样设置我的XAML:
And I set up my XAML like this:
<UserControl ... snip...>
<!-- Bind the textblock to whatever's in the DataContext -->
<TextBlock Text="{Binding}"></TextBlock>
</UserControl>
效果很好,我在执行应用程序时可以看到值"SomeStringOnlyAvailableAtRuntime".但是,使用Visual Studio 2008的XAML设计器在设计时看不到任何东西.
Works great, I can see the value "SomeStringOnlyAvailableAtRuntime" when I execute my application. However, I don't see anything at Design Time using Visual Studio 2008's XAML Designer.
在设计时如何查看文本块的占位符值(任何值)?
How can I see a placeholder value (anything) for the textblock at design time?
谢谢!
-迈克
推荐答案
在设计用户控件时,我经常在绑定上使用FallbackValue
进行查看.例如:
I often use FallbackValue
on the binding to have something to look at while I design user controls. For example:
<TextBlock Text={Binding Path=AverageValue, FallbackValue=99.99} />
但是,由于FallbackValue
不仅在设计时应用,因此如果出于其他原因要在运行时使用FallbackValue
,这可能不合适.
However, since FallbackValue
is not just applied at design time, this might not be appropriate if you want to use FallbackValue
at run time for other reasons.
这篇关于如何在WPF Visual Studio Designer中显示占位符值,直到可以加载实际值为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!