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
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));
Design-time data is mock data you set to make your controls easier to visualize in the XAML Designer.
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.
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.
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}"/>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With