【发布时间】:2014-06-12 11:44:13
【问题描述】:
我有一个包含 3 列的 DataGrid。
当用户在 DataGrid 的最后一个单元格上点击 Enter 时,我想向 DataGrid 添加一个新行。我已经使用DataGrid.InputBindings 成功完成了所需的所有操作,但这里的问题是,当我在第二列上按 Enter 键时,会添加一个新行。我希望在焦点位于属于第 3 列的单元格上并按 Enter 键时添加它。
这是我的代码:
<DataGrid CanUserAddRows="False" CanUserDeleteRows="True" CanUserReorderColumns="False" CanUserResizeColumns="False" AutoGenerateColumns="False"
ItemsSource="{Binding People}" SelectedItem="{Binding SelectedRow}" CurrentCell="{Binding SelectedCell, Mode=OneWayToSource}"
DataGridCell.GotFocus="DataGrid_CellGotFocus" SelectionMode="Single">
<DataGrid.InputBindings>
<KeyBinding Key="Enter" Command="{Binding DataContext.NewRowCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Confirmation" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding DataContext.SelectedConfirmation, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
Visibility="{Binding DataContext.ConfirmationVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Converter={StaticResource boolToVisibilityConverter}}">
<ComboBox.Items>
<sys:String>Add New</sys:String>
<sys:String>End Of List</sys:String>
</ComboBox.Items>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Name" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Age" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Age}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Age}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
在视图模型中:
public class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
People = new ObservableCollection<Person>();
People.Add(new Person());
NewRowCommand = new RelayCommand(NewRow);
}
private ObservableCollection<Person> _people;
public ObservableCollection<Person> People
{
get
{
return _people;
}
set
{
_people = value;
OnPropertyChanged("People");
}
}
private Person _selectedRow;
public Person SelectedRow
{
get
{
return _selectedRow;
}
set
{
_selectedRow = value;
OnPropertyChanged("SelectedRow");
if (_selectedRow == People.Last())
{
ConfirmationVisibility = true;
}
else
{
ConfirmationVisibility = false;
}
}
}
private bool _confirmationVisibility;
public bool ConfirmationVisibility
{
get
{
return _confirmationVisibility;
}
set
{
_confirmationVisibility = value;
OnPropertyChanged("ConfirmationVisibility");
}
}
private string _selectedConfirmation;
public string SelectedConfirmation
{
get
{
return _selectedConfirmation;
}
set
{
_selectedConfirmation = value;
OnPropertyChanged("SelectedConfirmation");
}
}
private DataGridCellInfo _selectedCell;
public DataGridCellInfo SelectedCell
{
get
{
return _selectedCell;
}
set
{
_selectedCell = value;
OnPropertyChanged("SelectedCell");
}
}
public ICommand NewRowCommand { get; set; }
private void NewRow(object obj)
{
if (SelectedRow == People.Last())
{
if (SelectedConfirmation == "Add New")
{
if (SelectedCell.Column.Header.ToString() == "Age")
{
People.Add(new Person());
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这是在 VS2012 中创建的示例项目:https://drive.google.com/file/d/0B5WyqSALui0bWVNHcVFXU1hOQ00/edit?usp=sharing
【问题讨论】: