Skip to content

DataGrid 数据绑定错误 #91

@yanxin-lz

Description

@yanxin-lz

WPF/.net 4.5
在将DataGrid进行数据更新时出现Xaml绑定错误。
虽然控件能正常使用,但是会因为绑定错误不断抛出导致性能骤降。
XAML代码
<DataGrid x:Name="数据库页" Grid.Row="1" Grid.Column="1" > </DataGrid>

C#代码(每次刷新页面都会调用此方法,但是每次调用,就会报xaml绑定失败)

    /// <summary>
    /// 读入一个DataSet,翻页并更新数据源
    /// </summary>
    /// <param name="页数"></param>
    /// <param name="复制数据源"></param>
   public void 翻页(int 页数, DataSet 复制数据源)
    {
        //清空所有行以准备刷新。
        临时表.Tables[0].Rows.Clear();
        //如果当前页数大于总页数则跳过,并设置为最后一页
        if (页数 > 复制数据源.Tables[0].Rows.Count / 展示条数 )
        {

            页面页数 = 复制数据源.Tables[0].Rows.Count / 展示条数;
            return;
        }
        //如果已经为第一页,则仍然设置返回第一页
        if (页数< 1)
        {

            页面页数 = 1;
            return;
        }
        //计算页面展示的起始条数
        int 起始条 = 展示条数 * (页数-1);

        //遍历需要展示的每一条数据并加入到要显示的临时表中
        for (int i = 起始条; i < 展示条数+起始条; i++)
        {
            if (i > 复制数据源.Tables[0].Rows.Count)
            {
                continue;
            }
            临时表.Tables[0].Rows.Add(复制数据源.Tables[0].Rows[i].ItemArray);
        }
        //将临时表重新绑定到显示页面
            数据库页.ItemsSource = 临时表.Tables[0].DefaultView;
        //更新页面指示器的条数
            页面指示器.Text = $"第{页数}页 / 共{复制数据源.Tables[0].Rows.Count / 展示条数}页";
    }

以下错误信息:

严重性	计数	数据上下文	绑定路径	目标	目标类型	说明	文件	行	项目
错误	1395	null	(0)	DataGridCell.SelectedBackground	Brush	找不到源: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1'。			
错误	1397	null	FontSize	DataGridCell.FontSize	Double	找不到源: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1'。			
错误	1396	null	(0)	DataGridCell.CellPadding	Thickness	找不到源: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1'。			
错误	1396	null	(0)	DataGridCell.ColumnVerticalContentAlignment	VerticalAlignment	找不到源: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1'。			
错误	1396	null	(0)	DataGridCell.ColumnHorizontalContentAlignment	HorizontalAlignment	找不到源: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1'。			

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions