我正在使用 Caliburn micro(1.3)/MVVM 和 Silverlight。当我更新 itemsource RadGridView 时,我丢失了选定的项目。我找到了一篇关于在实现 MVVM 时实现保存所选项目的行为的博客。我可以获取选定的项目,但一旦刷新 itemsource,我就无法将它们重新设置。有人可以告诉我如何使用 caliburn.micro 和 RadGridVIew 来实现它吗?我认为最好的方法是创建一个 caliburn 微约定,但我只能找到为 selectedItem 而不是 selectedItems 创建约定的参考。
有人可以告诉我如何做到这一点吗?我尝试了以下方法,但它不起作用。
private static void SetRadGridSelecteditemsConventions()
{
ConventionManager
.AddElementConvention<DataControl>(DataControl.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
.ApplyBinding = (viewModelType, path, property, element, convention) =>
{
ConventionManager.SetBinding(viewModelType, path, property, element, convention, DataControl.ItemsSourceProperty);
if (ConventionManager.HasBinding(element, DataControl.SelectedItemProperty))
return true;
var index = path.LastIndexOf('.');
index = index == -1 ? 0 : index + 1;
var baseName = path.Substring(index);
foreach (var selectionPath in
from potentialName in ConventionManager.DerivePotentialSelectionNames(baseName)
where viewModelType.GetProperty(potentialName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) != null
select path.Replace(baseName, potentialName))
{
var binding = new Binding(selectionPath) { Mode = BindingMode.TwoWay };
BindingOperations.SetBinding(element, DataControl.SelectedItemProperty, binding);
}
return true;
};
}
谢谢,斯蒂芬