如何正确地一起使用显式实现的接口属性和wpf可见性? [英] How do I use an explicitly implemented interface property and wpf visiblity together properly?

查看:109
本文介绍了如何正确地一起使用显式实现的接口属性和wpf可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

我有一些ViewModel对象,其中一些实现了接口ISomeInterface,有些则没有.接口公开了名为SomeEnumeration(IEnumerable<T>)的属性.

I have a few ViewModel objects, some of which implement an interface ISomeInterface, some don't. The interfaces exposes a property called SomeEnumeration (IEnumerable<T>).

例如:

public sealed class ViewModelA : ViewModelBase, ISomeInterface
{
    // ...

    IEnumerable<Foo> ISomeInterface.SomeEnumeration
    {
        get { ...; }
    }
}

public sealed class ViewModelB : ViewModelBase
{
    // ...
}

到目前为止,我的XAML的设计方式使得两个ViewModel都具有我要绑定的属性(即PropertyAPropertyB等).我还没有遇到我要设置为DataContext的ViewModel上不存在我要绑定的属性的情况.但是,现在,我将……并且它将针对明确实现的属性(我不确定这在WPF绑定引擎中是否有任何区别).

My XAML, so far, has been designed in a way that both of the ViewModels happen to have the properties I am binding against (i.e. PropertyA, PropertyB, etc.). I haven't ran into the situation yet where a property I am binding against does not exist on the ViewModels that I am setting as the DataContext. But, now I will... and it will be against a property that is explicitly implemented (I'm not sure if that makes any difference in the WPF Binding Engine).

基本上,我的xaml如下所示:

Basically, my xaml will look like the following:

<StackPanel
  Visiblity="{Binding Path=SomeEnumeration, Converter={StaticResource AnyConverter}">
    ...
</StackPanel>

我不确定这是否还能解决问题,因为:

I'm not sure if this will even work because:

  1. 不是每个DataContext都将包含该属性(如果不包含,则应将其隐藏)...在这种情况下我该怎么办?
  2. 对于确实包含该属性的DataContext,它是明确实现的...您必须先强制​​转换还是其他?
  1. Not every DataContext will contain the property (in case it doesn't, it should be hidden) ... What should I do in this case?
  2. For the DataContexts that do contain the property, it is explicitly implemented ... do you have to cast first or something?

推荐答案

通常,当您要使用WPF数据绑定引擎时,还需要使用 TargetNullValue 绑定属性.这些到底是做什么的?

Generally, when you want to use the WPF DataBinding Engine, you'll want to also utilize the FallbackValue and the TargetNullValue binding properties. What do these exactly do?

FallbackValue:在绑定无法执行时获取或设置值 返回值.
TargetNullValue:获取或设置使用的值 源中的值为 null 时在目标中输入.

FallbackValue: Gets or sets the value when the binding is unable to return a value.
TargetNullValue: Gets or sets the value that is used in the target when the value of the source is null.

Jon在此答案中很好地解释了绑定引擎:

Jon explains the binding engine pretty well in this answer:

Binding.DoNothing是您从中主动返回的对象实例 价值转换器;它指示绑定引擎不更新 目标属性的值.这是乔什的一个很好的例子 史密斯,你可能会用它做什么.

Binding.DoNothing is an object instance that you actively return from a value converter; it instructs the binding engine to not update the value of the target property at all. Here's a nice example by Josh Smith of what you might use this for.

FallbackValue是您在绑定上设置的属性;它可以让你 在以下情况下,指定要应用于目标属性的值:

FallbackValue is a property that you set on bindings; it allows you to specify the value to be applied to the target property if:

  • 无法解析绑定源(例如,错误的绑定路径),或者
  • 绑定属性值等于DependencyProperty.UnsetValue或
  • 用于绑定的值转换器引发异常,或者
  • 用于绑定的值转换器返回DependencyProperty.UnsetValue或
  • 由绑定管道产生的值对于目标属性(例如错误的类型)无效
  • the binding source cannot be resolved (e.g. wrong binding path), or
  • the binding property value is equal to DependencyProperty.UnsetValue, or
  • a value converter used for the binding throws an exception, or
  • a value converter used for the binding returns DependencyProperty.UnsetValue, or
  • the value produced by the binding pipeline is not valid for the target property (e.g. wrong type)

TargetNullValue也是您在绑定上设置的属性;它允许你 指定要应用于目标属性的值(如果该值) source属性的null.例如,如果将文本框绑定到 字符串属性TargetNullValue可让您选择出现在 源字符串为null的文本框.

TargetNullValue is also a property you set on bindings; it allows you to specify the value to be applied to the target property if the value of the source property is null. For example, if you bind a text box to a string property TargetNullValue lets you pick what appears in the text box if the source string is null.


关于绑定到明确实现的接口",真正的问题应该是如何设置接口属性的路径,因为如何实现该接口无关紧要.实际上,这在XAML中很容易做到,下面是一个示例:


As far as binding to "explicitly implemented interface", the real question should be how do you set the path to an interface property, because how that interface is implemented does not matter. This is actually quite easy to do in XAML, and here is an example:

<TextBox Text="{Binding Path=(local:ISomeInterface.SomeProperty)}" />


因此,直接回答您的问题:


So, to answer your questions directly:

  1. 使用FallbackValue(如有必要,还可以使用TargetNullValue).例如,当由于绑定错误而无法解析绑定值时,请传递null.
  2. 使用正确的模式将Path属性绑定到接口的属性(请参见上面的示例).
  1. Utilize FallbackValue (and optionally TargetNullValue if necessary). For example, pass in null when the binding value can not be resolved due to a binding error.
  2. Utilize the correct pattern for binding the Path property to an interface's property (see above example).

XAML用法:

<StackPanel Visiblity="{Binding Path=(local:ISomeInterface.SomeEnumeration),
                                Converter={StaticResource AnyConverter},
                                FallbackValue={x:Null}}">
    ...
</StackPanel>

最后一点:如果绑定提早失败,则空值FallbackValue不会是传递给转换器的值,无论绑定是在属性级别还是在转换器级别等失败,它都是最终使用的值.因此,不要期望在将 null 传递到转换器中时,转换器仍将运行.

One final note: If the binding fails early, the null FallbackValue would not be the value passed into the converter, it would be the final value used whether the binding fails at the property level or the converter level or etc. So do not expect that the converter will still run while passing in null into it.

这篇关于如何正确地一起使用显式实现的接口属性和wpf可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆