BeginEditCommand | F2 |
CancelEditCommand | Esc |
CommitEditCommand | Enter |
DeleteCommand | Delete |
Deleteキーがおされた時にデフォルトのイベントハンドラが呼ばれる前に自前の処理を行う。
xaml
<DataGrid PreviewKeyDown="dataGrid_PreviewKeyDown"> </DataGrid>
コードビハインド
private void dataGrid_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Delete) { var dataGrid = (DataGrid)sender; MessageBox.Show("DELETEキーが押されました"); e.Handled = true; //trueを設定するとデフォルト動作は行われない。 } }
DataGridComboBoxでElementStyleやEditingElementStyleを指定し、かつIsSynchronizedWithCurrentItem=Trueを使うと同列の他の行の表示がみだれるのでやめたほうがよいっぽい。
<DataGridComboBoxColumn Width="*" Header="変数の型"> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=Types}"/> <!-- これ --> <Setter Property="IsSynchronizedWithCurrentItem" Value="True"/> <Setter Property="DisplayMemberPath" Value="Label"/> <Setter Property="IsReadOnly" Value="True"/> <Setter Property="SelectedItem" Value="{Binding Type}"/> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=Types}"/> <!-- これ --> <Setter Property="IsSynchronizedWithCurrentItem" Value="True"/> <Setter Property="DisplayMemberPath" Value="Label"/> <Setter Property="SelectedItem" Value="{Binding Type}"/> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn>
DataGridColumnので設定する。以下の例ではValidatesOnDataErrorsがTrueなのでIDataErrorInfoを使って検証される。
<DataGridTextColumn Width="100" Header="名前" Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
RowValidationRulesにValidationRuleを設定する。
<DataGrid.RowValidationRules> <local:RowDataInfoValidationRule ValidationStep="UpdatedValue"/> </DataGrid.RowValidationRules>
<ResourceDictionary x:Class="ControlDemo.DataGridDemo.SingleClick.DataGridStyles" x:ClassModifier="public" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="DataGrid"> <!-- Your DataGrid style definition goes here --> <Setter Property="CellStyle"> <Setter.Value> <Style TargetType="DataGridCell"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/> </Style> </Setter.Value> </Setter> </Style> </ResourceDictionary>
namespace ControlDemo.DataGridDemo.SingleClick { partial class DataGridStyles : ResourceDictionary { // // SINGLE CLICK EDITING // private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridCell cell = sender as DataGridCell; if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } DataGrid dataGrid = FindVisualParent<DataGrid>(cell); if (dataGrid != null) { if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) cell.IsSelected = true; } else { DataGridRow row = FindVisualParent<DataGridRow>(cell); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } } } static T FindVisualParent<T>(UIElement element) where T : UIElement { UIElement parent = element; while (parent != null) { T correctlyTyped = parent as T; if (correctlyTyped != null) { return correctlyTyped; } parent = VisualTreeHelper.GetParent(parent) as UIElement; } return null; } } }
public class CustomDataGridTemplateColumn :DataGridTemplateColumn { protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) { editingElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); return base.PrepareCellForEdit(editingElement, editingEventArgs); } }