wpf mvvm 设置数据网格的焦点特定单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22195177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:07:35  来源:igfitidea点击:

wpf mvvm set focus particular cell of datagrid

c#wpfmvvmdatagrid

提问by Brux88

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?

我正在使用 mvvm - 我已经在谷歌上搜索了几个小时,我似乎找不到如何将焦点设置到单元格并将其置于编辑模式的示例。如果我有一个数据网格并且我想在视图模型的确定单元格中设置焦点。我能怎么做?

回答by Bizhan

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:

对于 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:

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.

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