XAML 中只读属性的 OneWayToSource 绑定 [英] OneWayToSource binding from readonly property in XAML

查看:34
本文介绍了XAML 中只读属性的 OneWayToSource 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OneWayToSource 作为模式绑定到 Readonly 属性,但似乎这不能在 XAML 中完成:

I'm trying to bind to a Readonly property with OneWayToSource as mode, but it seems this cannot be done in XAML:

<controls:FlagThingy IsModified="{Binding FlagIsModified, 
                                          ElementName=container, 
                                          Mode=OneWayToSource}" />

我明白了:

无法设置属性FlagThingy.IsModified",因为它没有可访问的集合访问器.

The property 'FlagThingy.IsModified' cannot be set because it does not have an accessible set accessor.

IsModifiedFlagThingy 上的只读 DependencyProperty.我想将该值绑定到容器上的 FlagIsModified 属性.

IsModified is a readonly DependencyProperty on FlagThingy. I want to bind that value to the FlagIsModified property on the container.

要清楚:

FlagThingy.IsModified --> container.FlagIsModified
------ READONLY -----     ----- READWRITE --------

这是否可以仅使用 XAML?

Is this possible using just XAML?

更新:好吧,我通过在容器上而不是在 FlagThingy 上设置绑定来解决这个问题.但我仍然想知道这是否可能.

Update: Well, I fixed this case by setting the binding on the container and not on the FlagThingy. But I'd still like to know if this is possible.

推荐答案

OneWayToSource 的一些研究成果...

Some research results for OneWayToSource...

选项 1.

// Control definition
public partial class FlagThingy : UserControl
{
    public static readonly DependencyProperty IsModifiedProperty = 
            DependencyProperty.Register("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata());
}

<controls:FlagThingy x:Name="_flagThingy" />

// Binding Code
Binding binding = new Binding();
binding.Path = new PropertyPath("FlagIsModified");
binding.ElementName = "container";
binding.Mode = BindingMode.OneWayToSource;
_flagThingy.SetBinding(FlagThingy.IsModifiedProperty, binding);

选项#2

// Control definition
public partial class FlagThingy : UserControl
{
    public static readonly DependencyProperty IsModifiedProperty = 
            DependencyProperty.Register("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata());

    public bool IsModified
    {
        get { return (bool)GetValue(IsModifiedProperty); }
        set { throw new Exception("An attempt ot modify Read-Only property"); }
    }
}

<controls:FlagThingy IsModified="{Binding Path=FlagIsModified, 
    ElementName=container, Mode=OneWayToSource}" />

选项#3(真正的只读依赖属性)

Option # 3 (True read-only dependency property)

System.ArgumentException: 'IsModified' 属性不能是数据绑定的.

System.ArgumentException: 'IsModified' property cannot be data-bound.

// Control definition
public partial class FlagThingy : UserControl
{
    private static readonly DependencyPropertyKey IsModifiedKey =
        DependencyProperty.RegisterReadOnly("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata());

    public static readonly DependencyProperty IsModifiedProperty = 
        IsModifiedKey.DependencyProperty;
}

<controls:FlagThingy x:Name="_flagThingy" />

// Binding Code
Same binding code...

Reflector 给出了答案:

Reflector gives the answer:

internal static BindingExpression CreateBindingExpression(DependencyObject d, DependencyProperty dp, Binding binding, BindingExpressionBase parent)
{
    FrameworkPropertyMetadata fwMetaData = dp.GetMetadata(d.DependencyObjectType) as FrameworkPropertyMetadata;
    if (((fwMetaData != null) && !fwMetaData.IsDataBindingAllowed) || dp.ReadOnly)
    {
        throw new ArgumentException(System.Windows.SR.Get(System.Windows.SRID.PropertyNotBindable, new object[] { dp.Name }), "dp");
    }
 ....

这篇关于XAML 中只读属性的 OneWayToSource 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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