Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:59:01+00:00 2026-06-12T06:59:01+00:00

Please help me, I am writing application with WPF Application Framework and EF Code

  • 0

Please help me, I am writing application with “WPF Application Framework” and EF Code First. I’m trying set selected row to ViewModels variable “SelectedRawMaterial” which is binded to DataGrids SelectedItem and it raises an exception : “The current value of the SelectionUnit property on the parent DataGrid prevents rows from being selected.”

private void rawMaterialTable_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        DataGridCell cell = null;
        try
        {
            cell = DataGridHelper.GetCell(rawMaterialTable.SelectedCells[0]);
        }
        catch (Exception)
        { }

        if (cell != null)
        {
            int i = DataGridHelper.GetRowIndex(cell);
            try
            {

                RawMaterial rm = (RawMaterial)rawMaterialTable.Items[i];
                ViewModel.SelectedRawMaterial = rm;
            }
            catch (Exception) { }
        }
    }



public static class DataGridHelper
{
    public static DataGridCell GetCell(DataGridCellInfo dataGridCellInfo)
    {
        if (!dataGridCellInfo.IsValid)
        {
            return null;
        }

        var cellContent = dataGridCellInfo.Column.GetCellContent(dataGridCellInfo.Item);
        if (cellContent != null)
        {
            return (DataGridCell)cellContent.Parent;
        }
        else
        {
            return null;
        }
    }

    public static int GetRowIndex(DataGridCell dataGridCell)
    {
        // Use reflection to get DataGridCell.RowDataItem property value.
        PropertyInfo rowDataItemProperty = dataGridCell.GetType().GetProperty("RowDataItem",
                                                                              BindingFlags.Instance |
                                                                              BindingFlags.NonPublic);

        DataGrid dataGrid = GetDataGridFromChild(dataGridCell);

        return dataGrid.Items.IndexOf(rowDataItemProperty.GetValue(dataGridCell, null));
    }

    public static DataGrid GetDataGridFromChild(DependencyObject dataGridPart)
    {
        if (VisualTreeHelper.GetParent(dataGridPart) == null)
        {
            throw new NullReferenceException("Control is null.");
        }
        if (VisualTreeHelper.GetParent(dataGridPart) is DataGrid)
        {
            return (DataGrid)VisualTreeHelper.GetParent(dataGridPart);
        }
        else
        {
            return GetDataGridFromChild(VisualTreeHelper.GetParent(dataGridPart));
        }
    }
}

In this place it raises exception.

ViewModel.SelectedRawMaterial = rm;

DataGrids code

<DataGrid x:Name="rawMaterialTable" ItemsSource="{Binding RawMaterials}" SelectedItem="{Binding SelectedRawMaterial}" 
              CanUserDeleteRows="False" BorderThickness="0" SelectionMode="Single" SelectionUnit="Cell" IsReadOnly="false"
              Grid.Row="1" Grid.Column="1" Margin="1,1,1,1" PreviewKeyDown="rawMaterialTable_PreviewKeyDown" SelectedCellsChanged="rawMaterialTable_SelectedCellsChanged" >
            <DataGrid.InputBindings>
                <KeyBinding Command="{Binding RemoveCommand}" Key="Del"/>
                <KeyBinding Command="{Binding AddCommand}" Key="Insert"/>
                <KeyBinding Command="{Binding EditCommand}" Key="F3"/>
            </DataGrid.InputBindings>

            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Code, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
                                  ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                                Header="{x:Static p:Resources.Code}" Width="60" ElementStyle="{StaticResource TextCellElementStyle}"
                                EditingElementStyle="{StaticResource TextCellEditingStyle}" DisplayIndex="0"/>


            </DataGrid.Columns>
        </DataGrid>

I added SelectionUnit=”Cell” because also I want to handle CellKeyDown.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:59:02+00:00Added an answer on June 12, 2026 at 6:59 am

    Its because you have the SelectionUnit (see the definition of the property) property of the data grid set to Cell and I believe you are trying to select a row at a time.

    Edited: If you change SelectionUnit to CellOrRowHeader to allow cell selection but the binding to select a whole row

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help, I'm stuck with objective-c global variables aka extern! I writing my first
Please help me, I'm trying to find some documentation about pre-installed applications at Android.
please help me to identify which of these following is more optimized code? for(int
Please help me understand the following code snippet :- def any(l): whether any number
I'm writing a WPF test application against a WCF REST service running on Azure
I am very new to XNA framework. I am writing a sample application in
I have a small-scale WPF application using VB.net as the code behind and I
I'm writing an application in classic ASP (yes, please forgive me) that sends e-mails
I have problem in writing the code in windows form application my problem is
I'm writing an InformationKiosk WPF application that displays different full screen slides (or views),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.