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

发布于 2024-10-05 08:22:42 字数 83 浏览 4 评论 0原文

我的绑定内容在 UI 设计模式中显示为空字符串。我想显示这些内容的一些虚假值,但我不知道该怎么做。

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有心的人 2024-10-12 08:22:42

在 Visual Studio 2010 中获取设计时数据的一种简单方法是使用设计数据上下文。带有 Window 和 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;
    }
}

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;
    }
}
守不住的情 2024-10-12 08:22:42

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

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

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'}"/>
雨巷深深 2024-10-12 08:22:42

您可以使用 DesignMode 属性来确定您是否处于设计时 ( http://msdn.microsoft.com/en-us/library/c58hb4bw(vs.71).aspx

对于解决此问题的方法有进一步的想法,但没有真正的结论:< a href="https://stackoverflow.com/questions/1889966/what-approaches-are-available-to-dummy-design-time-data-in-wpf">哪些方法可用于虚拟设计时数据WPF?

You could 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 further thoughts but no real conclusions on ways to do it at this question: What approaches are available to dummy design-time data in WPF?

一身仙ぐ女味 2024-10-12 08:22:42

您可以将内容包装在另一个属性中并测试该值是否为空。在这种情况下,返回您想要的假值。

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

You can wrap your content in another property and test if the value is empty. In that case return the fake value you want.

private string _content;
public string Content
{
    get
    { 
        if (_content != "") return _content;
        else return "FAKE";
    }
    set { _content= value; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文