c# - 如何正确使用显式实现的接口(interface)属性和 wpf 可见性?

标签 c# wpf data-binding datacontext explicit-interface

我有以下情况:

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

例如:

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

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

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

到目前为止,我的 XAML 的设计方式是两个 ViewModel 恰好具有我要绑定(bind)的属性(即 PropertyAPropertyB 等)。我还没有遇到过我要绑定(bind)的属性在我设置为 DataContext 的 ViewModel 上不存在的情况。 .但是,现在我会......并且它将针对显式实现的属性(我不确定这是否会对 WPF 绑定(bind)引擎产生任何影响)。

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

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

我不确定这是否有效,因为:

  1. 不是每个 DataContext将包含该属性(如果不包含,则应将其隐藏)......在这种情况下我应该怎么做?
  2. 对于 DataContext确实包含该属性的 s,它是明确实现的......你必须先转换还是什么?

最佳答案

通常,当您想要使用 WPF 数据绑定(bind)引擎时,您还需要利用 FallbackValueTargetNullValue绑定(bind)属性。这些到底有什么作用?

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 在 this answer 中很好地解释了绑定(bind)引擎:

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 is a property that you set on bindings; it allows you to specify the value to be applied to the target property if:

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


就绑定(bind)到“显式实现的接口(interface)”而言,真正的问题应该是如何设置接口(interface)属性的路径,因为如何实现该接口(interface)并不重要。这在 XAML 中实际上很容易做到,下面是一个示例:

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

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

  1. 利用 FallbackValue(必要时可选择使用 TargetNullValue)。例如,当由于绑定(bind)错误而无法解析绑定(bind)值时,传入 null。
  2. 使用正确的模式将 Path 属性绑定(bind)到接口(interface)的属性(参见上面的示例)。

XAML 用法:

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

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

关于c# - 如何正确使用显式实现的接口(interface)属性和 wpf 可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20075704/

相关文章:

c# - 如何在不知道哪个为空的情况下隐藏 gridview 中的空列?

c# - 如何通过向 .net 控制台应用程序发送请求并从中获取自定义响应来远程检查我的 .net 控制台应用程序是否正在运行

c# - 使用 Entity Framework 的 Repository 和 Unit Work 模式正确处理?

c# - 具有多个源数据和分组列的 WPF DataGrid

MVVM 中动态创建 MenuItem-Separator 的 WPF 光学样式

asp.net - ASP 存储过程到 GridView

c# - DataBinding 和 ErrorProvider - 如何提供自定义错误消息?

wpf - 在 MVVM 中调用 UserControl 的方法

c# - 如何覆盖默认窗口关闭操作?

wpf - 避免小窗口的 MVVM/数据绑定(bind)?