2017-06-19 54 views
0

我需要將綁定值發送到ValidationRule。我正在使用DependencyObject,但其值始終爲nullValidationRule和DependencyObject

這裏是我的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));  
} 

這裏是我的ValidationRule

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> 

當我通過驗證的MyTextBoxValidationRuleTxtVal.BindingValue一步總是null。我不知道爲什麼

編輯:

我改變DisplayValueDataUserTypes因爲似乎有一些混亂。

<Binding Path="DataUserTypes" 

這是文本框的Text值的綁定。我需要根據酒店ValidateValue驗證DataUserTypes。我試圖用DependencyObjectTxtVal來做到這一點。

我也修復了一個複製和粘貼類型。有一個叫TextValID的字段應該是TxtVal。對於那個很抱歉。

+1

'<地方:MyDependencyObject TEXTVALID = 「{綁定路徑= ValidateValue}」/>' - 什麼是TextValID這裏? – mechanic

+0

@ mechanic對不起,這是一個複製和粘貼錯字。請參閱更新的編輯 – Xaphann

回答

2
<local:MyTextBoxValidationRule ValidatesOnTargetUpdated="True" > 
    <local:MyTextBoxValidationRule.TxtVal > 
     <local:MyDependencyObject 
      BindingValue="{Binding ValidateValue, PresentationTraceSources.TraceLevel=High}" 
      /> 
    </local:MyTextBoxValidationRule.TxtVal> 
</local:MyTextBoxValidationRule> 

PresentationTraceSources.TraceLevel=High產生在VS輸出窗格中的下列跟蹤輸出:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=24230272) for Binding (hash=28316044) 
System.Windows.Data Warning: 58 : Path: 'ValidateValue' 
System.Windows.Data Warning: 60 : BindingExpression (hash=24230272): Default mode resolved to OneWay 
System.Windows.Data Warning: 61 : BindingExpression (hash=24230272): Default update trigger resolved to PropertyChanged 
System.Windows.Data Warning: 62 : BindingExpression (hash=24230272): Attach to Lines.MyDependencyObject.BindingValue (hash=37689768) 
System.Windows.Data Warning: 64 : BindingExpression (hash=24230272): Use Framework mentor <null> 
System.Windows.Data Warning: 67 : BindingExpression (hash=24230272): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=24230272): Framework mentor not found 
System.Windows.Data Warning: 65 : BindingExpression (hash=24230272): Resolve source deferred 
'Lines.exe' (CLR v4.0.30319: Lines.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
System.Windows.Data Warning: 67 : BindingExpression (hash=24230272): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=24230272): Framework mentor not found 

長話短說,的local:MyDependencyObject該實例沒有地方繼承從DataContext,因爲其父是不視覺樹。

你可以使用a BindingProxy來解決這個問題,但是你綁定了什麼? 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> 

綁定代理:

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)); 
} 
+0

我已將DisplayValue重命名爲DataUserTypes類型,並添加了一些評論,希望能夠清除我正在嘗試執行的操作。詳細信息請見編輯 – Xaphann

+1

'MyDependencyObject'沒有'TxtVal'屬性,但是你在它上面綁定了一個XAML。這是一個錯字嗎? –

+1

@Xaphann查看更新。 –

相關問題