Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value at design time XAML

I have a binded TextBlock, XAML:

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

I know the FallbackValue can be used if the Binding isn't available, but this happens at run time ? Is there any way to show a default value at design time ? It would make things easier if I could see a value when designing my windows instead of an empty TextBlock.

Thanks

like image 717
Sherlock Avatar asked Jun 20 '13 08:06

Sherlock


People also ask

How do I set default value for dependency property?

When you define a custom XAML dependency property, one of the things you do is specify the default value. You can do this by providing the default value directly: DependencyProperty. Register("MyProperty", propertyType, ownerType, new PropertyMetadata(defaultValue));

What is design time data?

Design-time data is mock data you set to make your controls easier to visualize in the XAML Designer.

What is DataContext WPF?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.

How do I see designs in XAML?

To open the XAML Designer, right-click a XAML file in Solution Explorer and choose View Designer. to switch which window appears on top: either the artboard or the XAML editor.


2 Answers

Update: Visual Studio 2019 v16.7

You can now do:

<TextBlock Text="{Binding MyText}" d:Text="Design time value"/> 

If you would prefer a less verbose version of Ian Bamforth's answer, you can just do

<TextBlock Text="{Binding MyText, FallbackValue=None}"/> 
like image 75
Scroog1 Avatar answered Sep 21 '22 00:09

Scroog1


Adapting an example from this question.

This works for me - the text "None" is shown in the designer:

 <TextBlock>     <TextBlock.Text>         <Binding ElementName="root" Path="blah" FallbackValue="None" />     </TextBlock.Text> </TextBlock> 

Hope that helps

like image 39
IBam Avatar answered Sep 22 '22 00:09

IBam