35

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

Sherlock
  • 5,557
  • 6
  • 50
  • 78
  • 4
    You can set a dummy ViewModel as the data context which should have that MyText property initialized to something. – Novitchi S Jun 20 '13 at 08:30
  • Possible duplicate of [How to see design-time data-binding in XAML editor (it works in runtime)?](https://stackoverflow.com/questions/16401885/how-to-see-design-time-data-binding-in-xaml-editor-it-works-in-runtime) – StayOnTarget Apr 09 '19 at 12:03

4 Answers4

62

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}"/>
Scroog1
  • 3,539
  • 21
  • 26
  • 5
    Is there a way to do a Design Time only fallback value? -- I don't want these values showing up if the property in my view model is null. – BrainSlugs83 Dec 08 '16 at 23:40
  • I suspect you'd need a proper design time viewmodel to fully handle that case. But if you are specifically worried about an existing property having a null value you could use the [TargetNullValue](https://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue(v=vs.110).aspx) property to point it out. – Scroog1 Dec 09 '16 at 10:38
  • 6
    d:Text sounds great but is currently limited to "WPF .NET Core and UWP projects" – jvcleave Aug 20 '20 at 17:10
  • 6
    @jvcleave: They've introduced a fix. This feature is also available for .NET Framework in the Preview channel. To enable it, go to Tools > Options > Environment > Preview Features, select New WPF XAML Designer for .NET Framework and then restart Visual Studio. – DefenestrationDay Mar 12 '21 at 12:11
8

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

Community
  • 1
  • 1
IBam
  • 10,114
  • 1
  • 24
  • 36
  • Could you explain ? Maybe i'm missing something but this shows how to set the FallBackValue on the Margin property ... ? – Sherlock Jun 20 '13 at 08:37
  • 9
    I downvoted this because the FallbackValue is the value that is shown when the binding couldn't be resolved. If you specify the datacontext (or datacontexttype) in XAML, this solution doesn't work, because the binding can be succesfully resolved (and in my case the element is collapsed, which I don't want in design time) – Geoffrey Aug 28 '14 at 13:29
6

Using FallbackValue is wrong, because it also affects the runtime behavior (the fallback value is used if the binding fails to obtain a value from the source).

I came up with a custom markup extension that mimics Binding (ideally I would have preferred to inherit from Binding, but the ProvideValue method is not virtual...):

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace MyNamespace
{
    public class BindingEx : MarkupExtension
    {
        private readonly Binding _binding;

        public BindingEx()
        {
            _binding = new Binding();
        }

        public BindingEx(string path)
        {
            _binding = new Binding(path);
        }

        public PropertyPath Path
        {
            get => _binding.Path;
            set => _binding.Path = value;
        }

        public BindingMode Mode
        {
            get => _binding.Mode;
            set => _binding.Mode = value;
        }

        public RelativeSource RelativeSource
        {
            get => _binding.RelativeSource;
            set => _binding.RelativeSource = value;
        }

        public string ElementName
        {
            get => _binding.ElementName;
            set => _binding.ElementName = value;
        }

        public IValueConverter Converter
        {
            get => _binding.Converter;
            set => _binding.Converter = value;
        }

        public object DesignValue { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
            if (target.TargetObject is DependencyObject d && DesignerProperties.GetIsInDesignMode(d))
                return DesignValue;

            return _binding.ProvideValue(serviceProvider);
        }
    }
}

You can use it just like Binding, with the addition of the DesignValue property:

<TextBlock Text="{my:BindingEx Name, DesignValue=John Doe}" />

Note that BindingEx doesn't have all the properties from Binding, but you can easily add them if necessary.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

If you have this data bound and are using the MVVM architecture then setting a DEFAULT value for the model item it is bound to will display the value at design time

I am just using:

Model.cs:

private int frame = 999999;
public int Frame
{
  get { return frame; }
  set
  {
    frame = value;
    NotifyPropertyChanged(m => m.Frame);
  }
}

and in my XAML:

 <TextBlock Text="{Binding Path=Frame}"  />

and the default value of "999999" is being displayed in the designer

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137