我需要做这样的事情: http ://social.msdn.microsoft.com/Forums/en-US/wpf/thread/982e2fcf-780f-4f1c-9730-cedcd4e24320/
我决定按照约翰史密斯的建议采取最好的方法。
我试图在 xaml 中设置绑定,但它不起作用(目标始终为空)。
我决定在代码中手动设置绑定(出于调试目的),所以我需要执行 DateRange 对象的“SetBinding”方法。
此方法在 DateRange 类型的对象中不存在。
有任何想法吗?
<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);
}
}
// 并在代码中绑定:
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);