Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a value to display for bound properties in WPF design mode?

My bound contents are showed as empty string in the UI design-mode. I want to display some faked value for those contents but I don't know how to.

Please share if you know how to. Thank you!

like image 240
Nam G VU Avatar asked Nov 30 '10 13:11

Nam G VU


People also ask

How do you change Inotify property?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

What is INotifyPropertyChanged in WPF?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

What is data binding in WPF?

Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.

What is binding source in WPF?

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property.


2 Answers

An easy way to get Design-time-data in Visual Studio 2010 is to use a design-datacontext. Short example with a Window and a ViewModel, For DataContext, the d:DataContext will be used in Design-mode and the StaticResource will be used in runtime. You can also use a separate ViewModel for design but in this example I will use the same ViewModel for both.

<Window ...
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:DesignTimeData"
        mc:Ignorable="d"            
        d:DataContext="{d:DesignInstance local:MyViewModel,
                        IsDesignTimeCreatable=True}">
    <Window.Resources>
        <local:MyViewModel x:Key="MyViewModel" />
    </Window.Resources>
    <Window.DataContext>
        <StaticResource ResourceKey="MyViewModel"/>
    </Window.DataContext>
    <StackPanel>
        <TextBox Text="{Binding MyText}"
                 Width="75"
                 Height="25"
                 Margin="6"/>
    </StackPanel>
</Window>

And in the ViewModels property MyText we check if we're in design mode and in that case we return something else.

public class MyViewModel : INotifyPropertyChanged
{
    public MyViewModel()
    {
        MyText = "Runtime-Text";
    }

    private string m_myText;
    public string MyText
    {
        get
        {
            // Or you can use
            // DesignerProperties.GetIsInDesignMode(this)
            if (Designer.IsDesignMode)
            {
                return "Design-Text";
            }
            return m_myText;
        }
        set
        {
            m_myText = value;
            OnPropertyChanged("MyText");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Designer.cs, which is found here, looks like this

public static class Designer
{
    private static readonly bool isDesignMode;
    public static bool IsDesignMode
    {
        get { return isDesignMode; }
    }
    static Designer()
    {
        DependencyProperty prop =
            DesignerProperties.IsInDesignModeProperty;
        isDesignMode =
            (bool)DependencyPropertyDescriptor.
                FromProperty(prop, typeof(FrameworkElement))
                      .Metadata.DefaultValue;
    }
}
like image 55
Fredrik Hedblad Avatar answered Oct 20 '22 17:10

Fredrik Hedblad


You can use FallbackValue property to display something in design time as well. But this will also be the value at runtime if your binding fails.

<TextBox Text="{Binding MyText, FallbackValue='My Fallback Text'}"/>
like image 8
John Avatar answered Oct 20 '22 16:10

John