XAML中的readonly属性的OneWayToSource绑定


87

我正在尝试Readonly使用OneWayToSourceas模式绑定到属性,但是似乎无法在XAML中完成此操作:

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

我得到:

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

IsModified是只读DependencyPropertyFlagThingy。我想将该值绑定到FlagIsModified容器上的属性。

要清楚:

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

仅使用XAML,这可能吗?


更新:嗯,我通过在容器而不是上设置绑定来解决这种情况FlagThingy。但是我仍然想知道这是否可能。


但是如何为只读属性设置值?
idursun

3
你不能 这也不是我想要达到的目标。我正在尝试从FROM只读属性IsModified改为readwrite属性FlagIsModified
Inferis

好问题。仅当容器为DependencyObject且FlagIsModified为DependencyProperty时,您的解决方法才有效。
乔什·G

10
很好的问题,但是我无法理解接受的答案。如果某些WPF专家能给我带来更多启发,我将不胜感激-这是错误还是针对每个设计?
奥斯卡(Oskar)

@Oskar认为是一个错误。虽然看不到修复。
user1151923 2013年

Answers:


45

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(真只读依赖项属性)

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

// 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给出答案:

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");
    }
 ....

30
所以这实际上是一个错误。
Inferis

好的研究。如果您在这里没有安排得那么好,那我将走同样的痛苦之路。同意@Inferis。
kevinarpe 2012年

1
这是一个错误吗?为什么只读的DependencyProperty不允许OneWayToSource绑定?
Alex Hope O'Connor

不是错误。这是设计使然并有据可查的。这是因为绑定引擎与依赖项属性系统一起工作的方式(绑定目标必须DependencyPropertyDP)。只读DP只能使用相关的进行修改DependencyPropertyKey。要注册BindingExpression引擎,引擎必须操纵目标DP的元数据。由于 DependencyPropertyKey为了保证公共写保护而被认为是私有的,因此引擎将不得不忽略此密钥,结果是无法在只读DP上注册绑定。
BionicCode

23

这是WPF的限制,它是设计使然。据报道在这里连接:
从只读依赖项属性的OneWayToSource绑定

我提出了一个解决方案,可以动态地将只读的依赖项属性推到PushBinding在此处写过博客的源中。下面的示例OneWayToSource将只读DP的绑定ActualWidth与对象ActualHeight的Width和Height属性绑定。DataContext

<TextBlock Name="myTextBlock">
    <pb:PushBindingManager.PushBindings>
        <pb:PushBinding TargetProperty="ActualHeight" Path="Height"/>
        <pb:PushBinding TargetProperty="ActualWidth" Path="Width"/>
    </pb:PushBindingManager.PushBindings>
</TextBlock>

PushBinding通过使用两个依赖项属性Listener和Mirror来工作。侦听器绑定OneWay到TargetProperty,并在PropertyChangedCallback其中更新Mirror属性,该属性绑定OneWayToSource到Binding中指定的任何内容。

演示项目可以在这里下载。
它包含源代码和简短的示例用法。


有趣!我想出了一个类似的解决方案,并将其称为“管道”-管道根据您的设计具有两个依赖项属性,并且具有两个单独的绑定。我曾经用过的用例是将原始的旧属性绑定到XAML中的原始的旧属性。
丹尼尔·保罗

3
我发现您的MS Connect链接不再起作用。这是否意味着MS已在.NET的较新版本中对其进行了修复,或者他们只是将其删除了?
分钟

不幸的是,@ Tiny Connect似乎最终被放弃了。它被链接到许多地方。我不认为这特别暗示着问题是否已解决。
UuDdLrLrSs

我正要写这个确切的东西。干得好!
aaronburro

5

写下:

用法:

<TextBox Text="{Binding Text}"
         p:OneWayToSource.Bind="{p:Paths From={x:Static Validation.HasErrorProperty},
                                         To=SomeDataContextProperty}" />

码:

using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

public static class OneWayToSource
{
    public static readonly DependencyProperty BindProperty = DependencyProperty.RegisterAttached(
        "Bind",
        typeof(ProxyBinding),
        typeof(OneWayToSource),
        new PropertyMetadata(default(Paths), OnBindChanged));

    public static void SetBind(this UIElement element, ProxyBinding value)
    {
        element.SetValue(BindProperty, value);
    }

    [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)]
    [AttachedPropertyBrowsableForType(typeof(UIElement))]
    public static ProxyBinding GetBind(this UIElement element)
    {
        return (ProxyBinding)element.GetValue(BindProperty);
    }

    private static void OnBindChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((ProxyBinding)e.OldValue)?.Dispose();
    }

    public class ProxyBinding : DependencyObject, IDisposable
    {
        private static readonly DependencyProperty SourceProxyProperty = DependencyProperty.Register(
            "SourceProxy",
            typeof(object),
            typeof(ProxyBinding),
            new PropertyMetadata(default(object), OnSourceProxyChanged));

        private static readonly DependencyProperty TargetProxyProperty = DependencyProperty.Register(
            "TargetProxy",
            typeof(object),
            typeof(ProxyBinding),
            new PropertyMetadata(default(object)));

        public ProxyBinding(DependencyObject source, DependencyProperty sourceProperty, string targetProperty)
        {
            var sourceBinding = new Binding
            {
                Path = new PropertyPath(sourceProperty),
                Source = source,
                Mode = BindingMode.OneWay,
            };

            BindingOperations.SetBinding(this, SourceProxyProperty, sourceBinding);

            var targetBinding = new Binding()
            {
                Path = new PropertyPath($"{nameof(FrameworkElement.DataContext)}.{targetProperty}"),
                Mode = BindingMode.OneWayToSource,
                Source = source
            };

            BindingOperations.SetBinding(this, TargetProxyProperty, targetBinding);
        }

        public void Dispose()
        {
            BindingOperations.ClearAllBindings(this);
        }

        private static void OnSourceProxyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            d.SetCurrentValue(TargetProxyProperty, e.NewValue);
        }
    }
}

[MarkupExtensionReturnType(typeof(OneWayToSource.ProxyBinding))]
public class Paths : MarkupExtension
{
    public DependencyProperty From { get; set; }

    public string To { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var provideValueTarget = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
        var targetObject = (UIElement)provideValueTarget.TargetObject;
        return new OneWayToSource.ProxyBinding(targetObject, this.From, this.To);
    }
}

尚未在样式和模板上对其进行测试,请猜测它需要特殊的大小写。


2

这是基于SizeObserver的另一个附加属性解决方案,在此详细介绍,将只读GUI属性推回到ViewModel中

public static class MouseObserver
{
    public static readonly DependencyProperty ObserveProperty = DependencyProperty.RegisterAttached(
        "Observe",
        typeof(bool),
        typeof(MouseObserver),
        new FrameworkPropertyMetadata(OnObserveChanged));

    public static readonly DependencyProperty ObservedMouseOverProperty = DependencyProperty.RegisterAttached(
        "ObservedMouseOver",
        typeof(bool),
        typeof(MouseObserver));


    public static bool GetObserve(FrameworkElement frameworkElement)
    {
        return (bool)frameworkElement.GetValue(ObserveProperty);
    }

    public static void SetObserve(FrameworkElement frameworkElement, bool observe)
    {
        frameworkElement.SetValue(ObserveProperty, observe);
    }

    public static bool GetObservedMouseOver(FrameworkElement frameworkElement)
    {
        return (bool)frameworkElement.GetValue(ObservedMouseOverProperty);
    }

    public static void SetObservedMouseOver(FrameworkElement frameworkElement, bool observedMouseOver)
    {
        frameworkElement.SetValue(ObservedMouseOverProperty, observedMouseOver);
    }

    private static void OnObserveChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = (FrameworkElement)dependencyObject;
        if ((bool)e.NewValue)
        {
            frameworkElement.MouseEnter += OnFrameworkElementMouseOverChanged;
            frameworkElement.MouseLeave += OnFrameworkElementMouseOverChanged;
            UpdateObservedMouseOverForFrameworkElement(frameworkElement);
        }
        else
        {
            frameworkElement.MouseEnter -= OnFrameworkElementMouseOverChanged;
            frameworkElement.MouseLeave -= OnFrameworkElementMouseOverChanged;
        }
    }

    private static void OnFrameworkElementMouseOverChanged(object sender, MouseEventArgs e)
    {
        UpdateObservedMouseOverForFrameworkElement((FrameworkElement)sender);
    }

    private static void UpdateObservedMouseOverForFrameworkElement(FrameworkElement frameworkElement)
    {
        frameworkElement.SetCurrentValue(ObservedMouseOverProperty, frameworkElement.IsMouseOver);
    }
}

声明控制权中的附加属性

<ListView ItemsSource="{Binding SomeGridItems}"                             
     ut:MouseObserver.Observe="True"
     ut:MouseObserver.ObservedMouseOver="{Binding IsMouseOverGrid, Mode=OneWayToSource}">    

1

这是绑定到Validation.HasError的另一种实现。

public static class OneWayToSource
{
    public static readonly DependencyProperty BindingsProperty = DependencyProperty.RegisterAttached(
        "Bindings",
        typeof(OneWayToSourceBindings),
        typeof(OneWayToSource),
        new PropertyMetadata(default(OneWayToSourceBindings), OnBinidngsChanged));

    public static void SetBindings(this FrameworkElement element, OneWayToSourceBindings value)
    {
        element.SetValue(BindingsProperty, value);
    }

    [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)]
    [AttachedPropertyBrowsableForType(typeof(FrameworkElement))]
    public static OneWayToSourceBindings GetBindings(this FrameworkElement element)
    {
        return (OneWayToSourceBindings)element.GetValue(BindingsProperty);
    }

    private static void OnBinidngsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((OneWayToSourceBindings)e.OldValue)?.ClearValue(OneWayToSourceBindings.ElementProperty);
        ((OneWayToSourceBindings)e.NewValue)?.SetValue(OneWayToSourceBindings.ElementProperty, d);
    }
}

public class OneWayToSourceBindings : FrameworkElement
{
    private static readonly PropertyPath DataContextPath = new PropertyPath(nameof(DataContext));
    private static readonly PropertyPath HasErrorPath = new PropertyPath($"({typeof(Validation).Name}.{Validation.HasErrorProperty.Name})");
    public static readonly DependencyProperty HasErrorProperty = DependencyProperty.Register(
        nameof(HasError),
        typeof(bool),
        typeof(OneWayToSourceBindings),
        new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    internal static readonly DependencyProperty ElementProperty = DependencyProperty.Register(
        "Element",
        typeof(UIElement),
        typeof(OneWayToSourceBindings),
        new PropertyMetadata(default(UIElement), OnElementChanged));

    private static readonly DependencyProperty HasErrorProxyProperty = DependencyProperty.RegisterAttached(
        "HasErrorProxy",
        typeof(bool),
        typeof(OneWayToSourceBindings),
        new PropertyMetadata(default(bool), OnHasErrorProxyChanged));

    public bool HasError
    {
        get { return (bool)this.GetValue(HasErrorProperty); }
        set { this.SetValue(HasErrorProperty, value); }
    }

    private static void OnHasErrorProxyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        d.SetCurrentValue(HasErrorProperty, e.NewValue);
    }

    private static void OnElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue == null)
        {
            BindingOperations.ClearBinding(d, DataContextProperty);
            BindingOperations.ClearBinding(d, HasErrorProxyProperty);
        }
        else
        {
            var dataContextBinding = new Binding
                                         {
                                             Path = DataContextPath,
                                             Mode = BindingMode.OneWay,
                                             Source = e.NewValue
                                         };
            BindingOperations.SetBinding(d, DataContextProperty, dataContextBinding);

            var hasErrorBinding = new Binding
                                      {
                                          Path = HasErrorPath,
                                          Mode = BindingMode.OneWay,
                                          Source = e.NewValue
                                      };
            BindingOperations.SetBinding(d, HasErrorProxyProperty, hasErrorBinding);
        }
    }
}

在xaml中的用法

<StackPanel>
    <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}">
        <local:OneWayToSource.Bindings>
            <local:OneWayToSourceBindings HasError="{Binding HasError}" />
        </local:OneWayToSource.Bindings>
    </TextBox>
    <CheckBox IsChecked="{Binding HasError, Mode=OneWay}" />
</StackPanel>

此实现特定于绑定 Validation.HasError


0

WPF不会使用CLR属性设置器,但是似乎基于它进行了一些奇怪的验证。

可能在您的情况下,这可以:

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

1
在这种情况下不使用CLR属性。
Inferis

您是说您刚刚定义了DependencyProperty并能够编写<controls:FlagThingy IsModified =“ ...” />?对我来说,如果我不添加CLR属性,则说:“ XML命名空间中不存在'IsModified'属性”。
alex2k8

1
我相信设计时会使用clr属性,因为运行时实际上直接进入了依赖属性(如果是)。
Meandmycode

在我的情况下,CLR属性是不必要的(我不使用代码中的IsModified),但是仍然存在(只有公共设置器)。仅在依赖属性注册时,设计时和运行时都可以正常工作。
Inferis

绑定本身不使用CLR属性,但是在XAML中定义绑定时,必须将其转换为代码。我猜想在此阶段XAML解析器会看到IsModified属性是只读的,并抛出异常(甚至在创建绑定之前)。
alex2k8 2009年

0

嗯...我不确定我是否同意这些解决方案。如何在属性注册中指定忽略外部更改的强制回调?例如,我需要实现一个只读的Position依赖项属性,以获取MediaElement控件在用户控件中的位置。这是我的操作方式:

    public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(double), typeof(MediaViewer),
        new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, OnPositionChanged, OnPositionCoerce));

    private static void OnPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var ctrl = d as MediaViewer;
    }

    private static object OnPositionCoerce(DependencyObject d, object value)
    {
        var ctrl = d as MediaViewer;
        var position = ctrl.MediaRenderer.Position.TotalSeconds;

        if (ctrl.MediaRenderer.NaturalDuration.HasTimeSpan == false)
            return 0d;
        else
            return Math.Min(position, ctrl.Duration);
    }

    public double Position
    {
        get { return (double)GetValue(PositionProperty); }
        set { SetValue(PositionProperty, value); }
    }

换句话说,只需忽略该更改并返回由没有公共修饰符的其他成员支持的值。-在上面的示例中,MediaRenderer实际上是私有MediaElement控件。


不幸的是,这对于BCL类的预定义属性不起作用:-/
或Mapper

0

解决此限制的方法是仅在类中公开Binding属性,并将DependencyProperty完全保留为私有。我实现了一个“ PropertyBindingToSource”只写属性(此属性不是DependencyProperty),可以在xaml中将其设置为绑定值。在此只写属性的设置器中,我调用BindingOperations.SetBinding将绑定链接到DependencyProperty。

对于OP的特定示例,它看起来像这样:

FlatThingy实现:

public partial class FlatThingy : UserControl
{
    public FlatThingy()
    {
        InitializeComponent();
    }

    public Binding IsModifiedBindingToSource
    {
        set
        {
            if (value?.Mode != BindingMode.OneWayToSource)
            {
                throw new InvalidOperationException("IsModifiedBindingToSource must be set to a OneWayToSource binding");
            }

            BindingOperations.SetBinding(this, IsModifiedProperty, value);
        }
    }

    public bool IsModified
    {
        get { return (bool)GetValue(IsModifiedProperty); }
        private set { SetValue(IsModifiedProperty, value); }
    }

    private static readonly DependencyProperty IsModifiedProperty =
        DependencyProperty.Register("IsModified", typeof(bool), typeof(FlatThingy), new PropertyMetadata(false));

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsModified = !IsModified;
    }
}

请注意,静态只读DependencyProperty对象是私有的。在控件中,我添加了一个按钮,其单击由Button_Click处理。在我的window.xaml中使用FlatThingy控件:

<Window x:Class="ReadOnlyBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ReadOnlyBinding"
    mc:Ignorable="d"
    DataContext="{x:Static local:ViewModel.Instance}"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <TextBlock Text="{Binding FlagIsModified}" Grid.Row="0" />
    <local:FlatThingy IsModifiedBindingToSource="{Binding FlagIsModified, Mode=OneWayToSource}" Grid.Row="1" />
</Grid>

请注意,我还实现了一个ViewModel来绑定到该模型,此处未显示。您可以从上面的源代码中收集一个名为“ FlagIsModified”的DependencyProperty。

它的工作原理非常好,允许我以松散耦合的方式将信息从View推回到ViewModel中,并明确定义了信息流的方向。


-1

您现在正在按错误的方向进行装订。每当IsModified更改您要创建的控件时,OneWayToSource都会尝试更新容器上的FlagIsModified。您需要相反的操作,即将IsModified绑定到container.FlagIsModified。为此,您应该使用绑定模式OneWay

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

枚举成员的完整列表:http : //msdn.microsoft.com/zh-cn/library/system.windows.data.bindingmode.aspx


5
不,我确实想要您所描述的我不想做的方案。FlagThingy.IsModified-> container.FlagIsModified
Inferis

3
因为发问者有一个模棱两可的问题而被打倒,这似乎有点矫kill过正。
JaredPar

6
@JaredPar:我看不出这个问题有什么歧义。该问题指出:1)有一个只读的依赖项属性IsIsModified,2)OP希望在XAML中声明对该属性的绑定,以及3)该绑定应在该OneWayToSource模式下工作。您的解决方案实际上不起作用,因为如问题所述,编译器不会让您声明只读属性的绑定,并且在概念上也不起作用,因为它IsModified是只读的,因此其值不能为更改(通过绑定)。
OR Mapper
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.