How to display placeholder value in WPF Visual Studio Designer until real value can be loaded

Mike picture Mike · Mar 29, 2009 · Viewed 8.4k times · Source

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

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

And I set up my XAML like this:

<UserControl ... snip...>
   <!-- Bind the textblock to whatever's in the DataContext -->
   <TextBlock Text="{Binding}"></TextBlock>
</UserControl>

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?

Thanks!

-Mike

Answer

Anthony Brien picture Anthony Brien · Mar 30, 2009

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} />

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.