当我单击可编辑的单元格时,我注意到 XamDataGrid 的 SelectedItems 属性为空。
如果它不可编辑,则 SelectedItems 集合会反映网格中所选记录的可视指示。
这是一个错误吗?当通过单击可编辑单元格选择它们时,是否有一种解决方法来获取 selectedItems?
这是一个反映这种行为的简单示例:
xml:
<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>
代码:
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;
}
}
如果我点击“Bond”“Miller”然后点击“James”,我会得到以下信息:
即使“詹姆斯邦德”记录显示为选中状态,它也不在 SelectedItems 集合中。
如果您显示有关所选记录的附加信息,这会非常烦人。