0

I tried to solve this problem several times. In a DataGrid I display data about financial transactions. One column is the Account class which is a navigation property (EF). I have a simple validation. The Account property should be not empty.

Here is the column:

    <DataGridTemplateColumn Header="{x:Static r:Resource.AccountName}" 
                                    CellTemplate="{StaticResource AccountTemplate}"
                                    CellEditingTemplate="{StaticResource AccountEditingTemplate}" />

Here are the DataTemplates:

    <DataTemplate x:Key="AccountEditingTemplate">
        <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
                            AncestorType={x:Type DataGrid}}, Path=DataContext.AccountObjects}" 
                  SelectedItem="{Binding Account, ValidatesOnDataErrors=True, UpdateSourceTrigger=LostFocus}" 
                  DisplayMemberPath="Name" Style="{StaticResource ComboBoxError}" IsEditable="True"
                  ToolTip="{Binding Account.Name}" />
    </DataTemplate>

    <DataTemplate x:Key="NameTemplate">
        <TextBlock Text="{Binding Name}" ToolTip="{Binding Name}"/>
    </DataTemplate>

    <DataTemplate x:Key="AccountTemplate">
        <ContentControl Content="{Binding Path=Account, ValidatesOnDataErrors=True}" 
                        ContentTemplate="{StaticResource NameTemplate}"/>
    </DataTemplate>

The CellEditingTemplate displays the ComboBox with the potential accounts. The CellTemplate is tricky. In AccountTemplate I bound Account so I properly get the validation error. Then I use the NameTemplate to to display the Account.Name as a TextBlock. (Previously I bound "Account.Name" but validating a nested property is not easy if Account is null (e.g. in a new row)).

If I want to change Account in an existing row then everything works.

enter image description here

If I want to change Account in a new row then the ComboBox does not appear! (One more confusing fact, If I click to the Account in the new line then the ComboBox appear if then I go to another column I can not get back the ComboBox)

enter image description here

UPDATE 1

My problem is that with double click I can not open the editing mode. Here is the validation (but the problem still happens without the validation)

    override public string this[string columnName]
    {
        get
        {
            var errorMessage = string.Empty;
            switch (columnName)
            {
                case "Instrument":
                    if (Instrument == null)
                        errorMessage = nameof(Instrument) + " " + GlobalValues.AppData["must be given!"];
                    break;
                case "Account":
                    if (Account == null)
                        errorMessage = nameof(Account) + " " + GlobalValues.AppData["must be given!"];
                    break;
            }
            return errorMessage;
        }
    }
Istvan Heckl
  • 864
  • 10
  • 22
  • You have to double-click the cell to put it into edit mode. This will display the `ComboBox`. And why don't you just add the `TextBlock` to `CellTemplate` directly, instead of the `ContentControl`? `NameTemplate` is not required. – BionicCode May 30 '20 at 12:34
  • Can you show your validation, please? – BionicCode May 30 '20 at 12:56
  • @BionicCode I have updated the question. If put directly a TextBlock into CellTemplate then I have to bind Account.Name. This works but at inserting a new line I not get the validation error, probably because it tries to validate Account.Name and not Account. – Istvan Heckl May 31 '20 at 05:11
  • Are you sure your templates resources resolve? Can you try to inline the templates instead of adding them to a resource dictionary? – BionicCode May 31 '20 at 13:00
  • You shouldn't trigger an error on new/placeholder lines. Only on user input. That's why validation is usually triggered on cell/row update. – BionicCode May 31 '20 at 13:12
  • `Account` can never be `null`once the user added a row. This validation is senseless. You should validate the user input e.g. to prevent empty cell`s when the user leaves the edit mode. You must decide whether you need to validate cells independently or the complete row. See [Microsoft Docs: How to: Implement Validation with the DataGrid Control](https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-implement-validation-with-the-datagrid-control#to-validate-multiple-values-in-a-single-row). – BionicCode May 31 '20 at 13:21
  • @BionicCode The `DataTemplate` resourse is resolved properly with an existing row, so it can not be a problem.The point of my validation is that you can not add a new row without specifying `Account`. Once you added a row the `ComboBox` makes it impossible to set null. – Istvan Heckl May 31 '20 at 13:32
  • If you have inline both cell templates AND removed the validation from the binding and still cannot edit the cell, then please post a minimal example that reproduces this issue; no database, minimal models etc. Because then the issue is in a code portion you didn't posted. While creating a minimal running example you may solve the problem by yourself. – BionicCode May 31 '20 at 14:14

0 Answers0