3
我正在一个WPF项目,我正在创建一些样式,其中之一是DataGridCell
样式,它工作正常。如何避免绑定错误,当父母被删除
我的问题是:当用户删除任何行时,许多错误都显示在Visual Studio的输出窗口中。
这是错误:
System.Windows.Data Warning: 4 : Cannot find source for binding with reference
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid',
AncestorLevel='1''.
BindingExpression:Path=CanUserAddRows; DataItem=null; target element is 'DataGridCell'
(Name=''); target property is 'NoTarget' (type 'Object')
所以,我想这个错误是因为当DataGridCell
从DataGrid
删除,绑定不找家长,但是,我能做些什么避免得到这些错误?我的意思是,我怎样才能建立绑定的条件?
我的XAML代码风格是如下:
<DataGrid Margin="6,25,6,35" x:Name="dataGrid">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=CanUserAddRows}" Value="False" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#A4A4A4"/>
</MultiDataTrigger>
. . . . .
希望有人能帮助我,在此先感谢。
感谢您的回复,我是WPF新手,我有一个问题,这个答案可以应用在'Condition'的'Binding'属性中? – Dante 2012-07-17 02:21:40
@Dante是的,你可以,这些是绑定类的属性,所以他们会为每一个绑定工作;为参考添加代码。 – akjoshi 2012-07-17 05:50:29
嗨akjoshi,我刚刚尝试了你的建议,但我仍然收到相同的错误 – Dante 2012-07-18 14:23:11