I'm using mvvm - I have been Googling this for a few hours, and I can't seem to find an example of how to set the focus to a cell and place it in edit mode. If I have a datagrid and I want set focus in a determinate cell from viewmodel. how can I do?
            Asked
            
        
        
            Active
            
        
            Viewed 6,930 times
        
    1 Answers
3
            From your general question I think this might solve the problem:
grid.Focus();
grid.CurrentCell = new DataGridCellInfo(grid.Items[rowIndex],grid.Columns[columnIndex]);
Edit
for MVVM pattern it would be like this:
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>
Now if you type the desired row index in first textbox and the desired column index in second one, and then click on focus button, it should focus on a cell.
to make sure it's working, after clicking focus, start typing something.
        Bizhan
        
- 16,157
 - 9
 - 63
 - 101
 
- 
                    1I know. It was a headache for me for quite a some time too. the only way I found to do it was like above. any other solution I tried failed. – Bizhan Mar 05 '14 at 11:12
 - 
                    3@Bizz - How about using attached property? In that case no code behind needed. Something like discussed [here](http://stackoverflow.com/a/1356781/632337). – Rohit Vats Mar 05 '14 at 11:23
 - 
                    
 - 
                    1I updated the answer. the only thing is ViewModel now must reference PresentationFramework.dll because of usage of DataGrid – Bizhan Mar 05 '14 at 12:32