我工作的一个WPF项目,我创造了一些款式,其中之一是 DataGridCell
的风格,它工作正常。
I am working on a WPF project, and I am creating some styles, one of them is the DataGridCell
style, it works fine.
我的问题是::当用户删除任何行,许多错误显示在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
删除,绑定不找到了父母,但是,我能做些什么,以避免这些错误?我的意思是,我怎么能建立一个条件,结合??
So, I guess the error is because when the DataGridCell
is removed from the DataGrid
, the binding does not find the the Parent, but, What can I do to avoid getting these errors?? I mean, how can I establish a condition for the binding??
我的XAML样式code是如下:
My XAML Style code is as following:
<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>
. . . . .
希望有人能帮助我,在此先感谢。
Hope someone can help me, thanks in advance.
我也遇到这类问题,并设定的 TargetNullValue 和的 FallbackValue 摆脱大部分时间这些绑定错误的。
I have also faced these kind of problems and setting TargetNullValue and FallbackValue gets rid of these binding errors most of the time.
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType= {x:Type DataGrid}}, Path=CanUserAddRows,
TargetNullValue=False, FallbackValue=False}" Value="False" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self},
Path=IsSelected, TargetNullValue=False,
FallbackValue=False}" Value="True" />
</MultiDataTrigger.Conditions>
在一般我也试着尽量少用的RelativeSource
尽可能使用的DataContext
在可能的情况。
In general I also try to minimize the use of RelativeSource
as much as possible, use DataContext
wherever possible.