views:

886

answers:

5

Hi everyone, 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

A: 

I don't know of a way to do this with Visual Studio's editor, but you can do this with Expression Blend.

Here's and article describing how to achieve this.

I do hope that MS merge the functionality of Blend and Visual Studio together because having one package do one thing and another something else is a bit silly. Especially when they're from the same company.

Cameron MacFarland
+6  A: 

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.

Anthony Brien
A: 

In your example you might need to use TargetNullValue, not FallbackValue as the binding expression is likely to be null as the DataContext is null at design time.

FallBackValue is used if the Path given in the binding does not exist, but as no path is specified I'd assume the DataContext would then be evaluated as null.

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

Also note that .NET Framework 3.5 SP1 is needed as these two additional properties were added in SP1.

Rhys
FallbackValue seems to work. Do you know why?
Patrick Szalapski
A: 

I'm trying to do this same thing, but I have:
    FallbackValue={StaticResource SomeHeavyWeightObject}

Every time I go to the screen, it creates another instance of SomeHeavyWeightObject. SomeHeavyWeightObject is defined in my App.xaml, so I figured it would only create it once and re-use it. I think it does the StaticResource lookup as soon as the .XAML file is loaded, even if the FallbackValue isn't used. That sorta defeats the purpose of it in my case.

Any idea for a workaround? I can't use DynamicResource on this object unfortunately.

A: 

Isn't the best option in this scenario to have a MultiValueConverter or ViewModel object handle the object load and update a dependency property for you?

Gusdor
Not sure I understand, can you explain a little bit more?
Mike
Rather than databinding to your state dependant resource directly, bind to an object that is responsible for loading said resource. When the XAML is first loaded, your resource will not be present and the binding will return an empty image, or an object that says 'loading...'. When the load is complete, an implementation of INotifyPropertyChanged should notify the databinding that the resource has changed to a loaded version.In this scenario it is probably worth authoring a Markup Extension for your loader type so that you can declare the whole operation inline.
Gusdor