Here's the documentation for Selection(WPF 或 Silverlight 相同)。
也可以进行程序化选择,有several examples here。
更具体地说,您可以使用实际项目来执行选择,而不是行。例如,假设您有一个模型Car,而您的视图模型有ObervableCollection<Car> Cars
您已将 GridView 绑定到该集合:
<telerik:GridView ItemsSource="{Binding Cars}"/>
这意味着您现在可以通过编程方式使用其中一个项目进行选择(比如单击按钮),就像选择第一个项目一样:
MyGridView.SelectedItems.Add(viewmodel.Cars[0]);
要回答有关基于表达式或视图模型属性启用和禁用选择的问题,您可以使用转换器,因为SelectionMode 是一个枚举。
假设您有一个名为 CanSelect 的视图模型属性
public CanSelect
{
get
{
return canSelect;
}
set
{
if(value==canSelect)return;
canSelect=value;
OnPropertyChanged();
}
}
接下来,您创建一个转换器:
public class MySelectionModeConverter
{
public Convert(object value...)
{
//pick the selection mode that makes the most sense for you
return (bool)value ? SelectionMode.Single : SelectionMode.None
}
}
现在您可以绑定到该视图模型属性,如下所示:
<telerik:GridView SelectionMode="{Binding CanSelect, Converter={StaticResource MySelectionModeConverter}}" />
最后,您声明您想要一个 RowClick 事件。默认情况下,这在 GridView 中不可用,通常您会使用SelectionChanged。您需要在 GridViewRow 上添加自己的 MouseDown 事件。
这是一个例子:
this.AddHandler(GridViewRow.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(OnMouseLeftDown), true);
public void OnMouseLeftDown(object sender, MouseButtonEventArgs e)
{ }