wpf mvvm设置了datagrid的特定单元格

时间:2014-03-05 10:34:30

标签: c# wpf mvvm datagrid

我正在使用mvvm - 我已经谷歌搜索了几个小时,我似乎找不到如何将焦点设置为单元格并将其置于编辑模式的示例。 如果我有一个datagrid,我想在viewmodel的确定单元格中设置焦点。 我怎么办?

1 个答案:

答案 0 :(得分:3)

从一般问题来看,我认为这可以解决问题:

grid.Focus();
grid.CurrentCell = new DataGridCellInfo(grid.Items[rowIndex],grid.Columns[columnIndex]);

修改

对于MVVM模式,它将是这样的:

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
    for (int i = 0; i < 10; i++)
    {
        Items.Add(new VM() { Text1=i.ToString(), Text2 = (i+100).ToString()});
    }
    FocusCommand = new MyCommand(o =>
    {
        var dg = o as DataGrid;
        if (dg != null) {
             dg.Focus();
             FocusedCell = new DataGridCellInfo(
                   dg.Items[FocusedRowIndex], dg.Columns[FocusedColumnIndex]);
        }
    });
}
//Items Observable Collection
public ObservableCollection<VM> Items { get { return _myProperty; } }
private ObservableCollection<VM> _myProperty = new ObservableCollection<VM>();
//FocusedCell Dependency Property
public DataGridCellInfo FocusedCell
{
    get { return (DataGridCellInfo)GetValue(FocusedCellProperty); }
    set { SetValue(FocusedCellProperty, value); }
}
public static readonly DependencyProperty FocusedCellProperty =
    DependencyProperty.Register("FocusedCell", typeof(DataGridCellInfo), typeof(MainWindow), new UIPropertyMetadata(null));
//FocusCommand Dependency Property
public MyCommand FocusCommand
{
    get { return (MyCommand)GetValue(FocusCommandProperty); }
    set { SetValue(FocusCommandProperty, value); }
}
public static readonly DependencyProperty FocusCommandProperty =
    DependencyProperty.Register("FocusCommand", typeof(MyCommand), typeof(MainWindow), new UIPropertyMetadata(null));
//FocusedRowIndex Dependency Property
public int FocusedRowIndex
{
    get { return (int)GetValue(FocusedRowIndexProperty); }
    set { SetValue(FocusedRowIndexProperty, value); }
}
public static readonly DependencyProperty FocusedRowIndexProperty =
    DependencyProperty.Register("FocusedRowIndex", typeof(int), typeof(MainWindow), new UIPropertyMetadata(0));
//FocusedColumnIndex Dependency Property
public int FocusedColumnIndex
{
    get { return (int)GetValue(FocusedColumnIndexProperty); }
    set { SetValue(FocusedColumnIndexProperty, value); }
}
public static readonly DependencyProperty FocusedColumnIndexProperty =
    DependencyProperty.Register("FocusedColumnIndex", typeof(int), typeof(MainWindow), new UIPropertyMetadata(0));

XAML:

<StackPanel Width="100">
    <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" 
              x:Name="datagrid"
              CurrentCell="{Binding FocusedCell}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="col1" Binding="{Binding Text1}"/>
            <DataGridTextColumn Header="col2" Binding="{Binding Text2}"/>
        </DataGrid.Columns>
    </DataGrid>
    <TextBox Text="{Binding FocusedRowIndex}" Margin="10"/>
    <TextBox Text="{Binding FocusedColumnIndex}" Margin="10"/>
    <Button Command="{Binding FocusCommand}" 
            CommandParameter="{Binding ElementName=datagrid}" Content="focus"/>
</StackPanel>

现在,如果您在第一个文本框中键入所需的行索引,在第二个文本框中键入所需的列索引,然后单击焦点按钮,则它应该关注单元格。

确保它正常工作,点击焦点后,开始输入内容。