在我的WPF应用程序中,当我水平滚动DataGrid时,Visual Studio 2010输出会打印此警告:
System.Windows.Data错误:5:BindingExpression产生的值是 对目标财产无效。值= ' - 0.29487179487171' BindingExpression:路径= CellsPanelHorizontalOffset;的DataItem = '数据网格' (名称= ''); target元素是'Button'(Name ='');目标属性是 '宽度'(类型'双')
我正在寻找datagrid模板定义;该异常应该由“Button”对象上的绑定引起:
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int offset = dy - ydy;//to adjust scrolling sensitivity of calling OnRefreshListener
ydy = dy;//updated old value
boolean shouldRefresh = (linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0)
&& (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) && offset > 30;
if (shouldRefresh) {
swipeRefreshLayout.setRefreshing(true);
} else {
swipeRefreshLayout.setRefreshing(false);
}
}
});
Width属性为Double,就像绑定中的CellsPanelHorizontalOffset属性一样。
我无法理解什么是错的,你能帮助我吗?感谢。
答案 0 :(得分:1)
错误的原因从描述中可以清楚地看出:
BindingExpression
生成的值对目标属性无效 值='-0.29487179487171
'...
目标元素是'Button
'; ...
target属性为“Width
”
因此,绑定到Button.Width
的数据值为-0.29487179487171
,但很明显,Width
不能为负数。但是,如果您使用Converter
从不传递负值,那么您只是隐藏真正的问题,即CellsPanelHorizontalOffset
首先应该永远不会是负数。
我只能假设您在自定义DataGrid
中使用了一些从DataGridCellsPanel
返回错误值的手动计算。来自MSDN上的DataGrid
Class页:
DataGridCellsPanel
:获取DataGridCellsPanel
的水平偏移量。
答案 1 :(得分:0)
我有同样的问题。事实证明,在我的情况下,罪魁祸首是将行标题宽度设置为零。问题在于DG_ScrollViewer中有一个按钮,其宽度是通过确定其父元素的宽度来设置的:Button Width =“ {Binding RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}},Path = CellsPanelHorizontalOffset}”。这会将按钮的宽度设置为负数,这不是有效宽度。我发现(目前)真正隐藏行标题的唯一方法是将行标题的宽度设置为零,然后重新编写ScrollViewer模板,这将消除错误并允许水平滚动工作。如果您的应用程序范围的数据网格设计没有行标题,那么这是一个很好的解决方案,否则您可能必须为不同的数据网格设置此xaml。希望这对某人有帮助。
<Style BasedOn="{StaticResource ControlBaseStyle}" TargetType="{x:Type DataGrid}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Border Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True"
>
<ScrollViewer Name="DG_ScrollViewer" Focusable="false">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
*********** hard code the width of this button *********
<Button x:Name="HijackThisButton"
Width="0"
Focusable="false"
Visibility="Collapsed"
/>
<DataGridColumnHeadersPresenter Name="PART_ColumnHeadersPresenter"
Grid.Column="1"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.Column}}"
/>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
Grid.Row="1"
Grid.ColumnSpan="2"
CanContentScroll="{TemplateBinding CanContentScroll}"
/>
<ScrollBar Name="PART_VerticalScrollBar"
Grid.Row="1"
Grid.Column="2"
Maximum="{TemplateBinding ScrollableHeight}"
Orientation="Vertical"
ViewportSize="{TemplateBinding ViewportHeight}"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
/>
<Grid Grid.Row="2" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollBar Name="PART_HorizontalScrollBar"
Grid.Column="1"
Maximum="{TemplateBinding ScrollableWidth}"
Orientation="Horizontal"
ViewportSize="{TemplateBinding ViewportWidth}"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
/>
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
</Style>