수색…


RowDoubleClick

RadGridView에서 행을 두 번 클릭하여 처리 할 수있는 일반적인 요청입니다. Telerik ( http://demos.telerik.com/silverlight/#GridView/ClickEvents )에서 제안한 솔루션은 코드 숨김을 기반으로합니다.

 this.grid.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);

MVVM을 사용하는 경우에는 클릭 한 행의 DataContext를 매개 변수로 전달하는 ICommand를 첨부하는 것이 좋습니다. 방법은 다음과 같습니다. 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;
    }
}

보기 ViewModel (Telerik DelegateCommand를 명령으로 사용하지만 원하는대로 할 수 있음) :

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; }
}

참고 : RadGridView의 ItemsSource에있는 유형이 무엇인지 알 수 있습니다. ICommand의 Execute 메서드에서 머리글이나 바닥 글을 클릭해도이 명령이 트리거되므로 명령에 대한 매개 변수의 형식을 확인하십시오. 이러한 시나리오에서는 매개 변수가 항목 중 하나가되지 않습니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow