【问题标题】:IsDropDownOpen in a DataGrid DataTemplate ComboBox in WPF MVVMIsDropDownOpen 在 WPF MVVM 中的 DataGrid DataTemplate ComboBox 中
【发布时间】:2012-05-04 08:43:34
【问题描述】:

我有一个 DataGrid,在 DataTemplate 中有一个 ComboBox

<DataGridTemplateColumn Header="Stock Name" Width="290">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding StockName}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox Width="290" Name="cmbStock" ItemsSource="{Binding Path=Stocks}" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" ></ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

当我使用 Tab 到达此 DataGridCell 时,我希望 ComboBox 为 DropDownOpen 这包括在我到达 DataGrid 单元格时使其处于编辑模式。

我正在使用 WPF MVVM

【问题讨论】:

  • 我不知道解决方案。我已经在组合框样式中设置了 IsDropDownOpen 属性,但是当我通过标签到达单元格时,单元格应该变得可编辑

标签: wpf xaml mvvm datagrid combobox


【解决方案1】:

我认为您需要做的是强制数据网格进入“单击或选项卡”编辑模式。基本上,当单元格聚焦时,会强制网格将 CellTemplate 切换为 CellEditingTemplate。代码是:

BeginEdit(); //dataGrid.BeginEdit()

现在,如何以及在何处连接它取决于您想要完成多少工作。您可以扩展 DataGrid 类并引入 DependencyProperty "SingleClickEdit" 或任何您想调用的名称。然后当监视器/预览键按下并在选项卡上选择单元格并强制它处于编辑模式。或者,如果您只需要该列,您可以监控:

<TextBlock Text="{Binding StockName}" 
           GotFocus="OnGotFocus" 
           PreviewKeyDown="OnPreviewKeyDown"
  ....., or something like that

然后在 .cs 代码中,例如在 OnGotFocus() 中,调用 datagrid.BeginEdit()。

编辑:(以下每 cmets/converation)

  • 将 SelectionChanged 处理程序添加到您的数据网格中
  • 将 IsDropDownOpen = true 添加到您的组合框

    <DataGrid x:Name="dataGrid" 
           SelectionChanged="dataGrid_SelectionChanged"
           ....>
    
    <ComboBox Width="290" Name="cmbStock" ItemsSource="{Binding Path=Stocks}" 
          ...
          IsDropDownOpen="True"></ComboBox>
    </DataTemplate>
    
  • 在.cs中

    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        dataGrid.BeginEdit();
    }
    

应该这样做,在我的测试中工作:),基本上你是在选择时强制数据网格进入编辑模式,在你的编辑模式下,你得到了已经打开的组合框

【讨论】:

  • 好的,让我快速输入一些内容进行测试,我会尽快回复您
  • 是的,您必须连接更多的东西才能将数据网格放入 EditMode。我写它的方式的主要问题是 TextBlock 没有收到焦点或键盘焦点。我正在查看我不久前完成的一些代码,在那里我扩展了 DataGrid,并编写了我自己的 selectCell() 方法,但它的代码很多..
  • 尽量保持简单,我有一个有趣的解决方案,我会在几分钟内发布它..
  • 关于我想问你的更多事情是,我想使用Enter 控制TAB,当我到达最后一列时,输入应该作为输入。
猜你喜欢
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 2012-06-19
相关资源
最近更新 更多