在我的 WPF 应用程序中,当我水平滚动 DataGrid 时,Visual Studio 2010 输出会打印此警告:
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='-0.29487179487171' BindingExpression:Path=CellsPanelHorizontalOffset; DataItem='DataGrid' (Name=''); target element is 'Button' (Name=''); target property is 'Width' (type 'Double')
我正在寻找数据网格模板定义;该异常应该是由“Button”对象上的绑定(bind)引起的:
<Button
Command="{x:Static DataGrid.SelectAllCommand}"
Focusable="false"
Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}"
Visibility="{Binding Path=HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
Width="{Binding Path=CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
Width 属性为 Double,类似于绑定(bind)中的 CellsPanelHorizontalOffset 属性。
我不明白出了什么问题,你能帮助我吗?谢谢。
最佳答案
从描述中可以清楚地看出错误的原因:
Value produced by
BindingExpression
is not valid for target property.;
Value='-0.29487179487171
' ...
target element is 'Button
'; ...
target property is 'Width
'
因此,绑定(bind)到Button.Width
的数据值为-0.29487179487171
,但显然,Width
不能为负数。但是,如果您在那里使用 Converter
来从不传递负值,那么您只是隐藏了真正的问题,即 CellsPanelHorizontalOffset
永远不应该为负值第一名。
我只能假设您在自定义 DataGrid
中使用了一些手动计算,该计算从 DataGridCellsPanel
返回了错误的值。来自 DataGrid
Class MSDN 页面:
DataGridCellsPanel
: Gets the horizontal offset for theDataGridCellsPanel
.
关于.net - 在数据网格中水平滚动时绑定(bind)警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749764/