I would like to send a value from a bound viewmodel to my inherited ValidationRule. I have read several articles but had no success implementing this.
Firstly my xaml.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:assets="clr-namespace:DiskManagerView.Assets"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"><ControlTemplate x:Key="validationTemplate"><DockPanel><Border BorderBrush="Red" BorderThickness="1" Margin="1"><AdornedElementPlaceholder/></Border></DockPanel></ControlTemplate><Style x:Key="textBoxInError" TargetType="{x:Type TextBox}"><Style.Triggers><Trigger Property="Validation.HasError" Value="true"><Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)/ErrorContent}"/></Trigger></Style.Triggers></Style><DataTemplate x:Key="EditTemplate"><Grid DataContext="{Binding}"
Name="Grid1"><Grid.RowDefinitions><RowDefinition Height="250*"/><RowDefinition Height="30"/></Grid.RowDefinitions><StackPanel Grid.Row="0"
Orientation="Vertical"><TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"><TextBox.Text><Binding Path="Name" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay"><Binding.ValidationRules><assets:NullStringValidationRule><assets:NullStringValidationRule.Content><assets:ValidationParams Value="{Binding Path=IsValid, Mode=TwoWay}"/></assets:NullStringValidationRule.Content></assets:NullStringValidationRule></Binding.ValidationRules></Binding></TextBox.Text></TextBox></StackPanel> </Grid></DataTemplate></ResourceDictionary>And my inherited ValidationRule and the Inherited DependencyObject.
Namespace Assets
Public Class NullStringValidationRule
Inherits ValidationRule
Private m_content As ValidationParams
Public Property Content() As ValidationParams
Get
Return m_content
End Get
Set(ByVal value As ValidationParams)
m_content = value
End Set
End Property
Public Overrides Function Validate(value As Object, cultureInfo As System.Globalization.CultureInfo) As System.Windows.Controls.ValidationResult
Dim _val As String = TryCast(value, String)
If String.IsNullOrEmpty(value.ToString) Then
'Content.Value = False
Return New ValidationResult(False, "NullStringValidationRule: String cannot be null.")
Else
'Content.Value = True
Return New ValidationResult(True, Nothing)
End If
End Function
End Class
Public Class ValidationParams
Inherits DependencyObject
Public Property Value As Object
Get
Return GetValue(ValueProperty)
End Get
Set(ByVal value As Object)
SetValue(ValueProperty, value)
End Set
End Property
Public Shared ReadOnly ValueProperty As DependencyProperty = _
DependencyProperty.Register("Value", _
GetType(Object), GetType(ValidationParams), _
New FrameworkPropertyMetadata(Nothing))
End Class
End Namespaceand the error message i receive.
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IsValid; DataItem=null; target element is 'ValidationParams' (HashCode=8674443); target property is 'Value' (type 'Object')
Can somebody set me straight on this, I'm stuck. I think i need to supply a DataContext to my ValidationParams but not sure.
Gary