I have a cell template for listview is defined as follows:
<DataTemplate x:Key="editableIPAddressColTemplate">
<Grid x:Name="dtTemplateGrid">
<TextBlock Width="100" FontSize="11" Text="{Binding ElementName=txt, Path=Text, Mode=TwoWay}"
ToolTip="{Binding ElementName=txt, Path=ToolTip, Mode=OneWay}"
Style="{StaticResource GridBlockStyle}">
</TextBlock>
<TextBox x:Name="txt" FontSize="11" Width="100" Style="{StaticResource GridEditStyle}"
Validation.Error="TextBox_Error" LostFocus="txt_LostFocus" >
<Binding Path="IPAddress" Mode="TwoWay" ValidatesOnDataErrors="True"
ValidatesOnExceptions="True" NotifyOnValidationError="True">
<Binding.ValidationRules>
<local:IPAddressValidationRule>
<local:IPAddressValidationRule.Params>
<local:ValidationParameters BoundListView="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}"/>
</local:IPAddressValidationRule.Params>
</local:IPAddressValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox>
</Grid>
</DataTemplate>
The IPAddressValidationRule is derived from ValidationRule and has a property called Params which is of type ValidationParameters. The ValidationParameters class is derived from dependency object and has a property called BoundListView which is of type ListView.
When I see through debugger, in side the IPValidationRule class, the BoundListView property is always null. What am I doing wrong?
I would greatly appreciate if anybody can help me figure out this.
Thanks a lot in advance.
You have a break in the tree, the validation rules just float about as property of a binding, you cannot use
ElementNameorRelativeSourcein such a disconnected place. Check the output window and you should be able to see some binding errors regarding this.You could try naming the
ListViewand set theBinding.Sourceusingx:Reference(x:Referencedoes not like cyclical dependencies, so you need to watch out for that)