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:
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.