繁体   English   中英

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

[英]wpf mvvm set focus particular cell of datagrid

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

从您的一般问题我认为这可能解决问题:

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>

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM