wpf 如何获得 SetBinding 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12036527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get SetBinding method
提问by John Wales
I need to do something like this: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/982e2fcf-780f-4f1c-9730-cedcd4e24320/
我需要做这样的事情:http: //social.msdn.microsoft.com/Forums/en-US/wpf/thread/982e2fcf-780f-4f1c-9730-cedcd4e24320/
I decided to follow the best way as John Smith advised.
我决定遵循约翰史密斯建议的最佳方式。
I tried to set binding in xaml, it didn't work (target was always null).
我试图在 xaml 中设置绑定,但它不起作用(目标始终为空)。
I decide to set binding manually in code (for debugging purpose), so I need to execute "SetBinding" method of the DateRange object.
我决定在代码中手动设置绑定(用于调试目的),所以我需要执行 DateRange 对象的“SetBinding”方法。
This method doesn't exists in object of type DateRange.
DateRange 类型的对象中不存在此方法。
Any ideas?
有任何想法吗?
<TextBox Grid.Row="1"
Grid.Column="1"
Name="Xml_Name"
>
<TextBox.Text>
<Binding XPath="@name" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<local:UniqueValidationRule x:Name="uniqueDatasourcesRule001" >
<local:UniqueValidationRule.UniqueCollection>
<local:UniqueDependencyObject uu="{Binding ElementName=Xml_Name, Path=Name, UpdateSourceTrigger=PropertyChanged}" />
</local:UniqueValidationRule.UniqueCollection>
</local:UniqueValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
public class UniqueDependencyObject : DependencyObject
{
public static readonly DependencyProperty uu11Property =
DependencyProperty.Register("uu", typeof(string), typeof(UniqueDependencyObject));
public string uu
{
set {
SetValue(uu11Property, value); }
get {
return (string)GetValue(uu11Property); }
}
}
public class UniqueValidationRule : ValidationRule
{
public UniqueDependencyObject UniqueCollection
{
get;
set;
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
// I set breakpoint to this line and check UniqueCollection.uu - it is always null
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
return new ValidationResult(true, null);
}
}
// And binding in code:
// 并在代码中绑定:
Binding binding = new Binding();
binding.ElementName = "Xml_Name";
binding.Path = new System.Windows.PropertyPath("Name");
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
UniqueValidationRule uVal = new UniqueValidationRule();
uVal.UniqueCollection = new UniqueDependencyObject();
BindingOperations.SetBinding(uVal.UniqueCollection, UniqueDependencyObject.uu11Property, binding);
回答by Clemens
I haven't read all the details of the forum post you're referring to, but i'm sure you need to know a few things about data binding before you can start using it.
我没有阅读您所指的论坛帖子的所有详细信息,但我确定您需要了解一些有关数据绑定的知识,然后才能开始使用它。
The target of a data binding is a dependency property
A dependency property has to be declared in a class that is derived from DependencyObject(at least when it is not an attached property, but we don't talk about those here)
The SetBindingmethod you're looking for is either a static method in BindingOperations, or a method of FrameworkElement.
数据绑定的目标是依赖属性
依赖属性必须在从DependencyObject派生的类中声明(至少当它不是附加属性时,但我们不在这里讨论那些)
您要查找的SetBinding方法要么是BindingOperations 中的静态方法,要么是FrameworkElement的方法。
So when you're going to set up a binding on some property of your DataRange class, it would have to be derived from DependencyObject, and you would set the binding like this:
因此,当您要在 DataRange 类的某些属性上设置绑定时,它必须从 DependencyObject 派生,并且您可以像这样设置绑定:
DataRange dataRange = ...
Binding binding = ...
BindingOperations.SetBinding(dataRange, DataRange.StartProperty, binding);
If DataRange were derived from FrameworkElement, you could write this:
如果 DataRange 派生自 FrameworkElement,您可以这样写:
dataRange.SetBinding(DataRange.StartProperty, binding);
Here DataRange.StartProperty is of type DependencyPropertyand represents the Startdependency property of class DataRange.
这里的 DataRange.StartProperty 是DependencyProperty类型,表示Start类 DataRange的依赖属性。
You should at least read the MSDN articles Data Binding Overview, Dependency Properties Overviewand Custom Dependency Properties.

