wpf 设计时 XAML 的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17208665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:02:47  来源:igfitidea点击:

Default value at design time XAML

wpfxamlbindingdefault

提问by Sherlock

I have a binded TextBlock, XAML:

我有一个绑定的TextBlockXAML:

<TextBlock Text="{Binding MyText}"/>

I know the FallbackValuecan 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.

我知道FallbackValue如果 Binding 不可用,则可以使用它,但这会在运行时发生吗?有没有办法在设计时显示默认值?如果我在设计窗口时可以看到一个值而不是一个空的TextBlock.

Thanks

谢谢

回答by Scroog1

If you would prefer a less verbose version of Ian Bamforth's answer, you can just do

如果您更喜欢Ian Bamforth 的答案的不那么冗长的版本,您可以这样做

<TextBlock Text="{Binding MyText, FallbackValue=None}"/>

回答by IBam

Adapting an example from thisquestion.

这个问题改编一个例子。

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

希望有帮助

回答by user3618238

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

如果您绑定了此数据并且正在使用 MVVM 架构,则为其绑定的模型项设置 DEFAULT 值将在设计时显示该值

I am just using:

我只是在使用:

Model.cs:

模型.cs:

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

and in my XAML:

在我的 XAML 中:

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

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

并且在设计器中显示“999999”的默认值

回答by Thomas Levesque

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

使用FallbackValue是错误的,因为它还会影响运行时行为(如果绑定无法从源获取值,则使用回退值)。

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

我想出了一个模仿的自定义标记扩展Binding(理想情况下我宁愿继承自Binding,但该ProvideValue方法不是虚拟的......):

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

您可以像 一样使用它Binding,并添加以下DesignValue属性:

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

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

请注意,BindingEx没有来自 的所有属性Binding,但您可以在必要时轻松添加它们。