How to set value to display related properties in WPF design mode?

My related content is displayed as an empty string in the user interface. I want to show some fake value for this content, but I don't know how to do it.

Please share if you know how to do this. Thanks!

+4
data-binding wpf
source share
4 answers

An easy way to get development-time data in Visual Studio 2010 is to use the constructor-datacontext. A short example with a window and ViewModel, for a DataContext, d: DataContext will be used in design mode, and StaticResource will be used at 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 MyText property, we check to see if we are in development mode, in which case we will 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 located here , is as follows

 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; } } 
+7
source share

You can use the FallbackValue property to display something at design time. But it will also be a value at runtime if your binding failed.

 <TextBox Text="{Binding MyText, FallbackValue='My Fallback Text'}"/> 
+3
source share

You can use the DesignMode property to find out if you are at design time ( http://msdn.microsoft.com/en-us/library/c58hb4bw(vs.71).aspx )

There are still thoughts, but there are no real conclusions on how to do this on this question: What approaches are available for dummy development-time data in WPF?

+2
source share

You can wrap your content in another property and check if it is empty. In this case, return the fake value you want.

 private string _content; public string Content { get { if (_content != "") return _content; else return "FAKE"; } set { _content= value; } } 
-3
source share

Source: https://habr.com/ru/post/649861/


All Articles