Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf datagrid datatable绑定和设置特定单元格';的工具提示和背景色_Wpf_Binding_Datagrid - Fatal编程技术网

Wpf datagrid datatable绑定和设置特定单元格';的工具提示和背景色

Wpf datagrid datatable绑定和设置特定单元格';的工具提示和背景色,wpf,binding,datagrid,Wpf,Binding,Datagrid,我将DataGrid绑定到DataTable,autogeneratecolumns=true,因为DataTable可以是Excel文件中的任何内容 但现在我想用红色绘制特定的单元格,并添加一个工具提示,当从Excel加载数据时,该提示是一条错误消息,可以出现在验证过程中 我的目标是把所有有错误的单元格涂成红色。错误在列表中其中CError可能有类似{row=4,col=6,Error=“日期错误”}的数据,因此某些单元格将有错误,我想用红色绘制这些单元格,并在工具提示中将错误附加到它们上,但

我将DataGrid绑定到DataTable,autogeneratecolumns=true,因为DataTable可以是Excel文件中的任何内容

但现在我想用红色绘制特定的单元格,并添加一个工具提示,当从Excel加载数据时,该提示是一条错误消息,可以出现在验证过程中

我的目标是把所有有错误的单元格涂成红色。错误在
列表中
其中CError可能有类似{row=4,col=6,Error=“日期错误”}的数据,因此某些单元格将有错误,我想用红色绘制这些单元格,并在工具提示中将错误附加到它们上,但我找不到方法。我的DataGrid定义如下:

在Viewmodel中,我有一个“LoadData()”方法,该方法在按下某个按钮时运行,该函数是:

public List<CError> ErrorInSheet {get;set;}

public void LoadData()
    {
        Sheets= GetSheetsFromExcel(PathExcel);
        SelectedSheet= Sheets.FirstOrDefault();
        MyDataTableFromExcel= LoadDataFromSheet(PathExcel, SelectedSheet);
        ErrorInSheet=ValidateData(MyDataTableFromExcel);

    }
我的问题是,在验证过程之后,如何仅将datagrid中有错误的单元格绘制为红色,这些错误将出现在ErrorInSheet集合中


我不知道我的方法是否不好,因为我还找不到绘制CErrorExcel类中指示的单元格的方法。

我确实找到了完成我之前要求的工作的方法,也许有一天会对某人有用 我确实使用viewmodel的方法从Excel加载数据,在该过程中我记录了一组错误,在加载scrollviwer的事件处理程序中,我将调用放在流程中,该流程将绘制数据网格的特定单元格,并设置工具提示。 这是密码

private async void ScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
    await CargaExcelEnGrid();
}


private async Task CargaExcelEnGrid()
{

    try
    {
        await vm.LoadDatafromExcel(); //loading in the viewmodel
        grdExcel.ItemsSource = null;
        grdExcel.ItemsSource = vm.TableExcel.DefaultView;

        foreach (CErrorExcel error in vm.ErrorExcel)
        {
            DataGridCell cell = GetCell(error.row, error.col, grdExcel);
            cell.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
            cell.ToolTip = error.Error;
        }
    }
    catch ( Exception err)
    {
        ModernDialog.ShowMessage(err.Message, "Error", MessageBoxButton.OK);
    }
}
public DataGridCell GetCell(int rowIndex, int columnIndex, DataGrid dg)
{
    DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
    if (row == null)
    {
        dg.UpdateLayout();
        dg.ScrollIntoView(dg.Items[rowIndex]);
        row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(rowIndex);
    }
    DataGridCellsPresenter p = GetVisualChild<DataGridCellsPresenter>(row);
    DataGridCell cell = p.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
    return cell;
}
private async void ScrollViewer_加载(对象发送方,路由目标)
{
等待CargaExcelEnGrid();
}
专用异步任务CargaExcelEnGrid()
{
尝试
{
等待vm.LoadDatafromExcel();//在viewmodel中加载
grdExcel.ItemsSource=null;
grdExcel.ItemsSource=vm.TableExcel.DefaultView;
foreach(vm.ErrorExcel中的CErrorExcel错误)
{
DataGridCell=GetCell(error.row、error.col、grdExcel);
cell.Foreground=新的SolidColorBrush(Color.FromRgb(255,0,0));
cell.ToolTip=error.error;
}
}
捕获(异常错误)
{
显示消息(err.Message,“Error”,MessageBoxButton.OK);
}
}
公共DataGridCell GetCell(int-rowIndex、int-columnIndex、DataGrid dg)
{
DataGridRow row=dg.ItemContainerGenerator.ContainerFromIndex(rowIndex)作为DataGridRow;
if(行==null)
{
dg.UpdateLayout();
dg.ScrollIntoView(dg.Items[rowIndex]);
行=(DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(行索引);
}
DataGridCellsPresenter p=GetVisualChild(行);
DataGridCell单元格=p.ItemContainerGenerator.ContainerFromIndex(columnIndex)作为DataGridCell;
返回单元;
}
工程真的很好,希望这将有助于别人

再见

private async void ScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
    await CargaExcelEnGrid();
}


private async Task CargaExcelEnGrid()
{

    try
    {
        await vm.LoadDatafromExcel(); //loading in the viewmodel
        grdExcel.ItemsSource = null;
        grdExcel.ItemsSource = vm.TableExcel.DefaultView;

        foreach (CErrorExcel error in vm.ErrorExcel)
        {
            DataGridCell cell = GetCell(error.row, error.col, grdExcel);
            cell.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
            cell.ToolTip = error.Error;
        }
    }
    catch ( Exception err)
    {
        ModernDialog.ShowMessage(err.Message, "Error", MessageBoxButton.OK);
    }
}
public DataGridCell GetCell(int rowIndex, int columnIndex, DataGrid dg)
{
    DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
    if (row == null)
    {
        dg.UpdateLayout();
        dg.ScrollIntoView(dg.Items[rowIndex]);
        row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(rowIndex);
    }
    DataGridCellsPresenter p = GetVisualChild<DataGridCellsPresenter>(row);
    DataGridCell cell = p.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
    return cell;
}