Wpf ValidationRule和DependencyObject

Wpf ValidationRule和DependencyObject,wpf,dependencyobject,validationrule,Wpf,Dependencyobject,Validationrule,我需要将绑定值发送到ValidationRule。我使用的是DependencyObject,但值总是null 这是我的DependencyObject public class MyDependencyObject : DependencyObject { public string BindingValue { get { return (string)GetValue(BindingValueProperty); } set { SetVa

我需要将绑定值发送到
ValidationRule
。我使用的是
DependencyObject
,但值总是
null

这是我的
DependencyObject

public class MyDependencyObject  : DependencyObject
{
    public string BindingValue
    {
        get { return (string)GetValue(BindingValueProperty); }
        set { SetValue(BindingValueProperty, value); }
    }
    public static readonly DependencyProperty BindingValueProperty =
        DependencyProperty.Register("BindingValue", typeof(string), typeof(MyDependencyObject), new UIPropertyMetadata(null));    
}
这是我的
验证规则

public class MyTextBoxValidationRule : ValidationRule
{
    private MyDependencyObject _TxtVal;
    public MyDependencyObject TxtVal
    {
        get { return _TxtVal; }
        set { _TxtVal = value; }
    }
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        //Validation Logic
    }   
}
以下是
XAML

<TextBox >
    <TextBox.Text>
        <Binding Path="DataUserTypes" 
            NotifyOnValidationError="True" 
            ValidatesOnDataErrors="True" 
            Mode="TwoWay" 
            UpdateSourceTrigger="PropertyChanged"  
            NotifyOnSourceUpdated="True" 
            NotifyOnTargetUpdated="True"
            Delay="100">
            <Binding.ValidationRules>
                <local:MyTextBoxValidationRule ValidatesOnTargetUpdated="True"  >
                    <local:MyTextBoxValidationRule.TxtVal >
                        <local:MyDependencyObject   
                            TxtVal="{Binding Path=ValidateValue}" />
                    </local:MyTextBoxValidationRule.TxtVal>
                </local:MyTextBoxValidationRule>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
这是textbox文本值的绑定。我需要基于属性
ValidateValue
验证
DataUserTypes
。我正试图用
DependencyObject
TxtVal
来实现这一点

我还修复了复制粘贴类型。有一个名为
TextValID
的字段,应该是
TxtVal
。很抱歉

<local:MyTextBoxValidationRule ValidatesOnTargetUpdated="True"  >
    <local:MyTextBoxValidationRule.TxtVal >
        <local:MyDependencyObject   
            BindingValue="{Binding ValidateValue, PresentationTraceSources.TraceLevel=High}" 
            />
    </local:MyTextBoxValidationRule.TxtVal>
</local:MyTextBoxValidationRule>
长话短说,
local:MyDependencyObject
的实例没有位置可以从中继承
DataContext
,因为它的父对象不在可视树中

你可以用它来解决这个问题,但你的目标是什么
ValidateValue
必须是某物的属性;但是什么呢?如果它是父视图模型的属性,则可以执行以下操作:

<TextBox.Resources>
    <local:BindingProxy
        x:Key="ViewmodelProxy"
        Data="{Binding}"
        />
</TextBox.Resources>
<TextBox.Text>
    <Binding Path="DataUserTypes" 
        NotifyOnValidationError="True" 
        ValidatesOnDataErrors="True" 
        Mode="TwoWay" 
        UpdateSourceTrigger="PropertyChanged"  
        NotifyOnSourceUpdated="True" 
        NotifyOnTargetUpdated="True"
        Delay="100">
        <Binding.ValidationRules>
            <local:MyTextBoxValidationRule ValidatesOnTargetUpdated="True"  >
                <local:MyTextBoxValidationRule.TxtVal>
                    <local:MyDependencyObject   
                        BindingValue="{Binding Data.ValidateValue, Source={StaticResource ViewmodelProxy}}" 
                        />
                </local:MyTextBoxValidationRule.TxtVal>
            </local:MyTextBoxValidationRule>
        </Binding.ValidationRules>
    </Binding>
</TextBox.Text>
长话短说,
local:MyDependencyObject
的实例没有位置可以从中继承
DataContext
,因为它的父对象不在可视树中

你可以用它来解决这个问题,但你的目标是什么
ValidateValue
必须是某物的属性;但是什么呢?如果它是父视图模型的属性,则可以执行以下操作:

<TextBox.Resources>
    <local:BindingProxy
        x:Key="ViewmodelProxy"
        Data="{Binding}"
        />
</TextBox.Resources>
<TextBox.Text>
    <Binding Path="DataUserTypes" 
        NotifyOnValidationError="True" 
        ValidatesOnDataErrors="True" 
        Mode="TwoWay" 
        UpdateSourceTrigger="PropertyChanged"  
        NotifyOnSourceUpdated="True" 
        NotifyOnTargetUpdated="True"
        Delay="100">
        <Binding.ValidationRules>
            <local:MyTextBoxValidationRule ValidatesOnTargetUpdated="True"  >
                <local:MyTextBoxValidationRule.TxtVal>
                    <local:MyDependencyObject   
                        BindingValue="{Binding Data.ValidateValue, Source={StaticResource ViewmodelProxy}}" 
                        />
                </local:MyTextBoxValidationRule.TxtVal>
            </local:MyTextBoxValidationRule>
        </Binding.ValidationRules>
    </Binding>
</TextBox.Text>

-什么文本在这里有效?@mechanical抱歉,这是一个复制粘贴的打字错误。查看更新的编辑
-此处的文本有效吗?@mechanical抱歉,这是一个复制粘贴错误。请参阅更新后的编辑我已将DisplayValue重命名为DataUserTypes类型,并添加了一些注释,希望能澄清我的意图。有关详细信息,请参见编辑
MyDependencyObject
没有
TxtVal
属性,但您在XAML中对其进行了绑定。那是打字错误吗?谢谢,就是这样!我已将DisplayValue重命名为DataUserTypes,并添加了一些注释,希望能澄清我的意图。有关详细信息,请参见编辑
MyDependencyObject
没有
TxtVal
属性,但您在XAML中对其进行了绑定。那是打字错误吗?谢谢,就是这样!
<TextBox.Resources>
    <local:BindingProxy
        x:Key="ViewmodelProxy"
        Data="{Binding}"
        />
</TextBox.Resources>
<TextBox.Text>
    <Binding Path="DataUserTypes" 
        NotifyOnValidationError="True" 
        ValidatesOnDataErrors="True" 
        Mode="TwoWay" 
        UpdateSourceTrigger="PropertyChanged"  
        NotifyOnSourceUpdated="True" 
        NotifyOnTargetUpdated="True"
        Delay="100">
        <Binding.ValidationRules>
            <local:MyTextBoxValidationRule ValidatesOnTargetUpdated="True"  >
                <local:MyTextBoxValidationRule.TxtVal>
                    <local:MyDependencyObject   
                        BindingValue="{Binding Data.ValidateValue, Source={StaticResource ViewmodelProxy}}" 
                        />
                </local:MyTextBoxValidationRule.TxtVal>
            </local:MyTextBoxValidationRule>
        </Binding.ValidationRules>
    </Binding>
</TextBox.Text>
public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}