问题描述
我有 EntitiesUserControl
负责 EntitiesCount
依赖项属性:
public static readonly DependencyProperty EntitiesCountProperty = DependencyProperty.Register(
nameof(EntitiesCount),
typeof(int),
typeof(EntitiesUserControl),
new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public int EntitiesCount
{
get { return (int)this.GetValue(EntitiesCountProperty); }
set { this.SetValue(EntitiesCountProperty, value); }
}
另一个(主要)控件包括 EntitiesUserControl
并通过绑定读取它的属性:
Another (primary) control include EntitiesUserControl
and read it property through binding:
<controls:EntitiesUserControl EntitiesCount="{Binding CountOfEntities, Mode=OneWayToSource}" />
CountOfEntities
属性仅在视图模型中存储和计数值的过程更改:
CountOfEntities
property in view model just store and process changing of count value:
private int countOfEntities;
public int CountOfEntities
{
protected get { return this.countOfEntities; }
set
{
this.countOfEntities = value;
// Custom logic with new value...
}
}
我需要 EntitiesUserControl
的 EntitiesCount
属性为只读(主要控件一定不要更改它,只需阅读即可),它之所以能如此工作,是因为明确声明了 Mode = OneWayToSource
。但是,如果声明 TwoWay
模式或不明确声明模式,则 EntitiesCount
可以从外部重写(至少正确绑定初始化后,因为它发生在指定的默认依赖项属性值之后。
I need EntitiesCount
property of EntitiesUserControl
to be read-only (primary control must not change it, just read) and it works this way only because Mode=OneWayToSource
declared explicitly. But if declare TwoWay
mode or don't explicitly declare mode, then EntitiesCount
could be rewritten from outside (at least right after binding initialization, because it happens after default dependency property value assigned).
我不能做合法的只读依赖项属性由于绑定限制(此中有最佳描述),因此我需要防止使用<$ c $以外的方式进行绑定c> OneWayToSource 。最好在OnlyOneWayToSource 标志,例如 BindsTwoWayByDefault
值system.windows.frameworkpropertymetadataoptions%28v = vs.110%29.aspx rel = nofollow noreferrer> FrameworkPropertyMetadataOptions
枚举...
I can't do 'legal' read-only dependency property due to binding limitations (best described in this answer), so I need to prevent bindings with mode other than OneWayToSource
. It would be best to have some OnlyOneWayToSource flag like BindsTwoWayByDefault
value in FrameworkPropertyMetadataOptions
enumeration...
有人建议如何实现这一目标吗?
Any suggestions how to achieve this?
推荐答案
这是一个有点 hacky,但是您可以创建一个 Binding
派生的类,并使用它代替 Binding
:
It's a „bit" hacky, but you can create a Binding
-derived class and use that instead of Binding
:
[MarkupExtensionReturnType(typeof(OneWayToSourceBinding))]
public class OneWayToSourceBinding : Binding
{
public OneWayToSourceBinding()
{
Mode = BindingMode.OneWayToSource;
}
public OneWayToSourceBinding(string path) : base(path)
{
Mode = BindingMode.OneWayToSource;
}
public new BindingMode Mode
{
get { return BindingMode.OneWayToSource; }
set
{
if (value == BindingMode.OneWayToSource)
{
base.Mode = value;
}
}
}
}
在XAML:
<controls:EntitiesUserControl EntitiesCount="{local:OneWayToSourceBinding CountOfEntities}" />
名称空间映射 local
可能是其他东西
The namespace mapping local
might be something else for you.
此 OneWayToSourceBinding
将 Mode
设置为 OneWayToSource
并阻止将其设置为其他任何内容。
This OneWayToSourceBinding
sets the Mode
to OneWayToSource
and prevents setting it to anything else.
这篇关于仅允许OneWayToSource绑定模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!