Dan*_*nte 5 .net wpf xaml binding styles
我正在一个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')
Run Code Online (Sandbox Code Playgroud)
因此,我猜错是因为,当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>
. . . . .
Run Code Online (Sandbox Code Playgroud)
希望有人能帮助我,在此先感谢。
我也遇到过此类问题,并且设置TargetNullValue和FallbackValue在大多数情况下可以消除这些绑定错误。
<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>
Run Code Online (Sandbox Code Playgroud)
RelativeSource一般来说,我也是尽量少用,DataContext能用就用。
| 归档时间: |
|
| 查看次数: |
1210 次 |
| 最近记录: |