Click here to Skip to main content
16,022,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello everyone,

I’m currently working on a WPF project where I need to display borehole deviation surveys using a UserControl and custom DataTemplates. However, I’m encountering an issue with the CustomDataGrid inside the UserControl. The specific error I’m facing is:

Value cannot be null. Parameter name: viewService


I’ve done the following to troubleshoot:

Ensured that my DataContext is properly set.
Verified that my ResourceDictionaries (where the DataTemplates are defined) are correctly merged.
Tried different ways to initialize the BindingGroup and bindings.
Here’s a brief summary of the structure:

I’m using a CustomDataGrid with columns that bind to a ViewModel.
The DataTemplates are referenced from an external ResourceDictionary.
The DataContext is set to a ViewModel that contains TrajectoryMeasurements, which is the ItemsSource for the grid.
Here’s a simplified version of the XAML for the UserControl:

XML
<UserControl x:Class="Mapsa.WellSchematic2.Views.Well.BoreholeDeviationSurveysView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ui="https://slb.production.engineering.ui"
    ...>
    <Grid>
        <ui:CustomDataGrid Name="SurveyGrid" ItemsSource="{Binding Path=TrajectoryMeasurements}" />
    </Grid>
</UserControl>

In the code-behind, I’m setting up the grid:
C#
public partial class BoreholeDeviationSurveysView : UserControl
{
    public BoreholeDeviationSurveysView()
    {
        InitializeComponent();
    }

    private void SurveyGridLoaded(object sender, RoutedEventArgs e)
    {
        if (DataContext is BoreholeDeviationSurveysViewModel)
        {
            SurveyGrid.BindingGroup = new BindingGroup { NotifyOnValidationError = true };
            SurveyGrid.BindingGroup.BeginEdit();
            SurveyGrid.BindingGroup.ValidationRules.Add(new BoreholeDeviationSurveyValidation((BoreholeDeviationSurveysViewModel)DataContext));
            SurveyGrid.BindingGroup.CommitEdit();
        }
    }
}

Despite following the proper setup, I still encounter the viewService error when interacting with the grid.

Has anyone experienced something similar? Any suggestions on what could be causing this error, particularly around viewService?

Thanks in advance!

What I have tried:

Verified that DataContext is properly set.
Ensured that ItemsSource is bound to a valid collection.
Confirmed the DataTemplates are correctly referenced in the ResourceDictionary.

However, the issue persists, and the error still appears when loading the CustomDataGrid.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900