.net - 在数据网格中水平滚动时绑定(bind)警告

标签 .net wpf visual-studio binding wpfdatagrid

在我的 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)中的 CellsPanelHorizo​​ntalOffset 属性。

我不明白出了什么问题,你能帮助我吗?谢谢。

最佳答案

从描述中可以清楚地看出错误的原因:

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 来从不传递负值,那么您只是隐藏了真正的问题,即 CellsPanelHorizo​​ntalOffset 永远不应该为负值第一名。

我只能假设您在自定义 DataGrid 中使用了一些手动计算,该计算从 DataGridCellsPanel 返回了错误的值。来自 DataGrid Class MSDN 页面:

DataGridCellsPanel: Gets the horizontal offset for the DataGridCellsPanel.

关于.net - 在数据网格中水平滚动时绑定(bind)警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749764/

相关文章:

c# - 当我试图解析的文本框中没有输入任何内容时,应用程序崩溃

c# - 向 Excel 表格添加一行

c# - Windows 窗体 - ErrorProvider + DataGridView

wpf - DataGrid RowDeleted 事件在哪里?

c# - Windows 无法在本地计算机上启动服务错误 5 访问被拒绝

c# - 命名空间中不存在 wpf 资源字典?

c# - WPF Multibinding RelativeSource Findancestor 评估性能

c# - 将旧的 VS 2002 Windows 窗体项目转换为新版本

.net - 混合 MarshalByRefObject 和 Serializable

c# - 在 Windows 中监听端口是否比在 Linux 中慢?