Question

I noticed that the SelectedItems Property of the XamDataGrid is empty when I click on a cell that is editable.

If it is not editable the SelectedItems Collection reflects the visual indication of selected records in the Grid.

Is this a bug? Is there a workaround to get the selectedItems when they were selected by clicking on the editable cell?

Here is a simple example to reflect this behaviour:

xaml:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <TextBox Name="TextBox1"></TextBox>
    <DataPresenter:XamDataGrid Grid.Column="1" Name="XamDataGrid" >
        <DataPresenter:XamDataGrid.FieldLayouts>
            <DataPresenter:FieldLayout>
                <DataPresenter:Field Name="FirstName" />
                <DataPresenter:Field Name="LastName" >
                    <DataPresenter:Field.Settings>
                        <DataPresenter:FieldSettings AllowEdit="False"/>
                    </DataPresenter:Field.Settings>
                </DataPresenter:Field>
            </DataPresenter:FieldLayout>
        </DataPresenter:XamDataGrid.FieldLayouts>
    </DataPresenter:XamDataGrid>
</Grid>

Code:

   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
         var persons = new List<Person>();
         persons.Add(new Person(){FirstName = "Jim",LastName = "Miller"});
         persons.Add(new Person(){FirstName = "James",LastName = "Bond"});
         XamDataGrid.DataSource = persons;
         XamDataGrid.SelectedItemsChanged += PrintNewSelection;
      }

      private void PrintNewSelection(object sender, SelectedItemsChangedEventArgs e)
      {
         foreach (ISelectableItem selectedItem in XamDataGrid.SelectedItems)
         {
            object item=null;
            if (selectedItem is Cell)
            {
               item = (selectedItem as Cell).Record.DataItem;

            }
            else if (selectedItem is DataRecord)
            {
               item = (selectedItem as DataRecord).DataItem;
            }
            TextBox1.Text += item + "\r\n";
         }
      }
   }

   internal class Person
   {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public override string ToString()
      {
         return FirstName + " " + LastName;
      }
   }

If I click "Bond" "Miller" and then "James" I get the following:

enter image description here

Even though the "James Bond" Record is displayed as selected it is not in the SelectedItems Collection.

This is very irritating if you display additional information about the selected Record.

Was it helpful?

Solution

I asked the Infragistics support as well and their solution was to handle the EditModeStarting event for the Grid and set the IsSelected Property for the Cell to true:

    private void XamDataGrid_EditModeStarting_1(object sender, EditModeStartingEventArgs e)
    {
        e.Cell.IsSelected = true;
    }

OTHER TIPS

The behavior you are seeing is expected and the record is only Active when you allow entering edit mode. Since you are looking to display additional information about the selected record, I am assuming that you only want to select a single record and you could use the ActiveRecord instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top