如何通过列表视图作为参数传递给细胞模板的验证规则 [英] How to pass list view as parameter to the validation rule of a cell template
问题描述
我有列表视图小区模板定义如下:
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>
该IPAddressValidationRule从有效性规则派生并有一个名为params属性是类型ValidationParameters的。该ValidationParameters类是从依赖对象派生,并有一个名为BoundListView属性,它是类型的ListView。
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.
当我通过调试器看到,在一边IPValidationRule类中,BoundListView属性总是空。我究竟做错了什么?
When I see through debugger, in side the IPValidationRule class, the BoundListView property is always null. What am I doing wrong?
我将不胜AP preciate如果有人可以帮助我弄清楚这一点。
I would greatly appreciate if anybody can help me figure out this.
非常感谢在前进。
推荐答案
您在树中的一个突破,验证规则只是漂浮大约为绑定,则不能使用的ElementName $财产C $ C>或
的RelativeSource
在这样的地方断开。检查输出窗口,你应该能够看到这方面的一些绑定错误。
You have a break in the tree, the validation rules just float about as property of a binding, you cannot use ElementName
or RelativeSource
in such a disconnected place. Check the output window and you should be able to see some binding errors regarding this.
您可以尝试命名的ListView
和 Binding.Source
使用的 X:参考
( X:参考
不喜欢周期性的依赖关系,所以你需要提防)
You could try naming the ListView
and set the Binding.Source
using x:Reference
(x:Reference
does not like cyclical dependencies, so you need to watch out for that)
这篇关于如何通过列表视图作为参数传递给细胞模板的验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!