如何在WPF设计模式下为绑定属性设置显示值?

9

在UI设计模式下,我的绑定内容显示为空字符串。我想为这些内容显示一些虚假的值,但我不知道如何操作。

如果您知道如何操作,请分享。谢谢!

4个回答

12

在Visual Studio 2010中获取设计时数据的简单方法是使用设计数据上下文(design-datacontext)。以下是一个使用窗口和ViewModel的简短示例。在DataContext中,d:DataContext将在设计模式下使用,而StaticResource将在运行时使用。您也可以为设计使用单独的ViewModel,但在此示例中,我将同时使用相同的ViewModel。

<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>

在ViewModels属性的MyText中,我们检查是否处于设计模式,如果是,我们返回其他内容。

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文件位于这里,它的外观是这样的

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;
    }
}

9

您可以使用FallbackValue属性在设计时显示一些内容。但是,如果绑定失败,这也将是运行时的值。

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

这是最简单的解决方案,但似乎不再起作用(至少在VS2017中)。 - Zeus
@Zeus 在VS2022中它有效。 - Gavin Coates
@Zeus 在VS2022中可以正常运行。 - undefined

2

-4
你可以将你的内容包裹在另一个属性中,并测试该值是否为空。在这种情况下,返回你想要的虚假值。
private string _content;
public string Content
{
    get
    { 
        if (_content != "") return _content;
        else return "FAKE";
    }
    set { _content= value; }
}

在运行时不会做同样的事情吗? - Jens
@Guy:请在另一个答案中发布您的答案,以便它可以被接受。 - Nam G VU
我不认为你的建议适合我 :(. 在运行时,“FAKE”将替换“”值 - 这是不期望的! - Nam G VU
@Nam Gi VU,抱歉,但我认为这就是你想要的:“我想为那些内容(空字符串)显示一些虚假值。” 你只需要用你的虚假值替换“FAKE”即可。 - Nicolas
我明白了。只是想确认我理解正确——您的“Content”属性不能有“string.Empty”值,对吗(在设计模式和运行时都是如此)?如果是的话,那不是我要找的(只需要设计模式的值)。 - Nam G VU

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接