Xceed DataGrid:使用键盘导航编辑单元格

时间:2017-09-14 11:14:24

标签: wpf datagrid keyboard xceed keyboard-navigation

我有一个datagrid,它使用DataTemplateSelector将不同的可编辑值显示为文本框,组合框或滑块。问题是我可以选中这些单元格,但无法用键盘编辑它们。我曾经想过,一旦细胞聚焦,我就可以编辑里面的物品了。

示例:Tab键到textbox-cell>开始打字 示例:Tab to slider-cell>现在重点关注>使用箭头键编辑

现在我必须用鼠标点击才能开始编辑。没有办法只使用键盘开始编辑。

这是我的代码:

public static MimeMessage createEmail(String to,
                                          String from,
                                          String subject,
                                          String bodyText)
            throws MessagingException {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        MimeMessage email = new MimeMessage(session);

        email.setFrom(new InternetAddress(from));
        email.addRecipient(javax.mail.Message.RecipientType.TO,
                new InternetAddress(to));
        email.setSubject(subject);
        email.setText(bodyText);
        return email;
    }

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我最终使用了Xceed的CellEditors。如果你想用键盘进行编辑,似乎无法绕过它们。

将EditValueVar列更改为:

                    <xcdg:Column FieldName="EditValueVar" Title="Edit Value" Width="150" ReadOnly="False"
                             CellEditorSelector="{StaticResource SettingsCellEditorSelector}"/>

注意:删除了DisplayMemberBinding,因为它可能导致动态数据出现问题

以下是我现在使用的CellEditors:

    <xcdg:CellEditor x:Key="maskEditor">
    <xcdg:CellEditor.EditTemplate>
        <DataTemplate>
            <xcdg:MaskedTextBox Mask="{Binding EditMask}"
                                Text="{Binding Path=(xcdg:Cell.ParentCell).DataContext.EditValue, 
                                               RelativeSource={RelativeSource Self}, 
                                               IsAsync=False, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnExceptions=True}"
                                Style="{StaticResource {x:Type xcdg:MaskedTextBox}}"/>
            <!--Text="{Binding EditValueText, IsAsync=False, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnExceptions=True}"/>-->
        </DataTemplate>
    </xcdg:CellEditor.EditTemplate>
    <xcdg:CellEditor.ActivationGestures>
        <xcdg:TextInputActivationGesture />
    </xcdg:CellEditor.ActivationGestures>
</xcdg:CellEditor>

<xcdg:CellEditor x:Key="comboEditor">
    <xcdg:CellEditor.EditTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Path=(xcdg:Cell.ParentCell).DataContext.SelectionValues, 
                                            RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}"
                      SelectedValuePath="Value"
                      SelectedValue="{Binding Path=(xcdg:Cell.ParentCell).DataContext.SelectionValue,
                                              RelativeSource={RelativeSource Self}, 
                                              UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                      DisplayMemberPath="ValueText"
                      Style="{StaticResource {x:Type ComboBox}}"
                      ItemContainerStyle="{StaticResource {x:Type ComboBoxItem}}"/>
        </DataTemplate>
    </xcdg:CellEditor.EditTemplate>
    <xcdg:CellEditor.ActivationGestures>
        <xcdg:KeyActivationGesture SystemKey="Down"
                                   Modifiers="Alt" />
        <xcdg:KeyActivationGesture Key="Up"
                                   Modifiers="Alt" />
        <xcdg:KeyActivationGesture Key="F4" />
        <xcdg:KeyActivationGesture Key="Space" />
    </xcdg:CellEditor.ActivationGestures>
</xcdg:CellEditor>

<xcdg:CellEditor x:Key="sliderEditor">
    <xcdg:CellEditor.EditTemplate>
        <DataTemplate>
            <Slider Value="{Binding Path=(xcdg:Cell.ParentCell).DataContext.EditSliderValue, 
                            RelativeSource={RelativeSource Self}, 
                            Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    Minimum="{Binding Path=(xcdg:Cell.ParentCell).DataContext.MinRangeValue, RelativeSource={RelativeSource Self}}"
                    Maximum="{Binding Path=(xcdg:Cell.ParentCell).DataContext.MaxRangeValue, RelativeSource={RelativeSource Self}}"
                    Style="{StaticResource {x:Type Slider}}"
                    VerticalAlignment="Bottom" 
                    IsSnapToTickEnabled="True"
                    TickFrequency="1"
                    Margin="0,0,0,0"/>
        </DataTemplate>
    </xcdg:CellEditor.EditTemplate>
    <xcdg:CellEditor.ActivationGestures>
        <xcdg:KeyActivationGesture SystemKey="Right"
                                Modifiers="Alt" />
        <xcdg:KeyActivationGesture Key="Left"
                                Modifiers="Alt" />
        <xcdg:KeyActivationGesture Key="F4" />
        <xcdg:KeyActivationGesture Key="Space" />
    </xcdg:CellEditor.ActivationGestures>
</xcdg:CellEditor>

你可能会注意到奇怪的(xcdg:Cell.ParentCell) - 正在发生的事情,是的,它像罪一样丑陋,但这是这样做的合法方式。您可能只是使用xcdg:CellEditorBinding来获得更好的运气。

xcdg:CellEditorBinding相当于这样的绑定:{Binding Path=(xcdg:Cell.ParentCell).Content, Mode=TwoWays, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}

我在内容之前添加了 .DataContext ,因为我的数据是如何构建的。

希望这有助于某人!