telerik
Telerik WPF RadGridView
Ricerca…
RowDoubleClick
È una richiesta comune essere in grado di gestire un doppio clic su una riga in un oggetto RadGridView. La soluzione proposta da Telerik ( http://demos.telerik.com/silverlight/#GridView/ClickEvents ) si basa sul codice sottostante:
this.grid.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
se utilizzi MVVM, probabilmente preferiresti collegare un ICommand, che verrà passato come parametro al DataContext della riga su cui è stato fatto clic. Ecco come farlo. XAML:
<telerik:RadGridView
ItemsSource="{Binding Rows}"
IsReadOnly="True"
ShowGroupPanel="False"
ShowColumnFooters="True"
local:RadGridViewAttachedProperties.RowDoubleClickCommand="{Binding DoubleClickRow}"/>
AttachedProperty:
public static class RadGridViewAttachedProperties
{
public static readonly DependencyProperty RowDoubleClickCommandProperty =
DependencyProperty.RegisterAttached("RowDoubleClickCommand", typeof(ICommand), typeof(RadGridViewAttachedProperties), new UIPropertyMetadata(null, OnRowDoubleClickCommandChanged));
public static ICommand GetRowDoubleClickCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(RowDoubleClickCommandProperty);
}
public static void SetRowDoubleClickCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(RowDoubleClickCommandProperty, value);
}
private static void OnMouseDoubleClick(object sender, RoutedEventArgs e)
{
var obj = sender as DependencyObject;
if (obj != null)
{
var command = GetRowDoubleClickCommand(obj);
if (command != null)
{
var frameworkElement = e.OriginalSource as FrameworkElement;
var dataContext = frameworkElement?.DataContext;
if (command.CanExecute(dataContext))
{
command.Execute(dataContext);
}
}
}
}
private static void OnRowDoubleClickCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var controlIsTheRightType = d as RadGridView;
if (controlIsTheRightType == null)
{
return;
}
controlIsTheRightType.MouseDoubleClick += OnMouseDoubleClick;
}
}
Esempio ViewModel (utilizza un comando Telegistrato come il comando, ma puoi fare ciò che vuoi):
public class MainWindowViewModel
{
public MainWindowViewModel()
{
Rows = new ObservableCollection<string>();
Rows.Add("kljshndfoa");
DoubleClickRow = new DelegateCommand(e => MessageBox.Show("Hello " + e));
}
public ICommand DoubleClickRow { get; }
public ObservableCollection<string> Rows { get; }
}
NOTA: saprai quale è il tipo di elementi nella risorsa Items di RadGridView. Nel metodo Execute del tuo ICommand, assicurati di controllare quale sia il tipo di parametro per il comando, poiché facendo clic sull'intestazione o sul piè di pagina si attiverà anche questo comando; ma in quegli scenari, il parametro non sarà uno dei tuoi articoli.