简体   繁体   中英

WPF - Selected Row is not changed after changing the day in a Datepicker control and pressing ENTER

I'm using a Datepicker control inside a DataGrid. After changing a day and pressing the ENTER key, the Selected row remains on the same Datagrid row and does not change to the next Datagrid row. Below is my DatePicker control:

<DataGridTemplateColumn x:Name="startDateColumn" Width="*" Header="Start Date">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding StartDate, StringFormat=d}"/>
       </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
   <DataGridTemplateColumn.CellEditingTemplate>
       <DataTemplate>
           <DatePicker SelectedDate="{Binding StartDate...
       </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

I want to allow the user to move to the next Datagrid row after selecting a day and pressing ENTER.

How can I do that?

Thanks in advance...

Add this attached property class

public class EnterKeyTraversal
{
    public static bool GetIsEnabled(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsEnabledProperty);
    }

    public static void SetIsEnabled(DependencyObject obj, bool value)
    {
        obj.SetValue(IsEnabledProperty, value);
    }

    static void ue_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        var ue = e.OriginalSource as FrameworkElement;
        var parent = GetVisualParent<DataGrid>(ue);
        if (parent == null) return;
        if (e.Key == Key.Enter)
        {
            e.Handled = true;
            ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
            parent.SelectedIndex += 1;
        }
    }

    private static void ue_Unloaded(object sender, RoutedEventArgs e)
    {
        var ue = sender as FrameworkElement;
        if (ue == null) return;

        ue.Unloaded -= ue_Unloaded;
        ue.PreviewKeyDown -= ue_PreviewKeyDown;
    }

    public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.RegisterAttached("IsEnabled", typeof(bool),

        typeof(EnterKeyTraversal), new UIPropertyMetadata(false, IsEnabledChanged));

    static void IsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var ue = d as FrameworkElement;
        if (ue == null) return;

        if ((bool)e.NewValue)
        {
            ue.Unloaded += ue_Unloaded;
            ue.PreviewKeyDown += ue_PreviewKeyDown;
        }
        else
        {
            ue.PreviewKeyDown -= ue_PreviewKeyDown;
        }
    }
    public static T GetVisualParent<T>(DependencyObject child) where T : Visual
    {
        Visual parentObject = VisualTreeHelper.GetParent(child) as Visual;
        if (parentObject == null) return null;
        return parentObject is T ? parentObject as T : GetVisualParent<T>(parentObject);
    }
}

Add style for DatePickerTextBox

    <Style TargetType="{x:Type DatePickerTextBox}">
        <Setter Property="local:EnterKeyTraversal.IsEnabled" Value="True"/>
    </Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM