2012-04-23 74 views
2

我有一个组合框一个DataGrid在一个DataTemplateIsDropDownOpen在DataGrid的DataTemplate组合框在WPF MVVM

<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到DropDownOpen这包括在使DataGrid单元格在编辑模式下,当我达到它。

我使用WPF MVVM

+0

什么人有你试过了吗? – 2012-04-23 18:01:25

+0

我不知道这个解决方案。我已经在组合框样式中设置了IsDropDownOpen属性,但为此,当我的标签到达单元格 – 2012-04-23 18:02:55

回答

2

我想你需要做的是给力的数据网格成“单点击或选项卡”编辑模式。基本上,当单元格聚焦时,强制网格将CellTemplate切换到CellEditingTemplate。该代码是:

BeginEdit(); //dataGrid.BeginEdit() 

现在在哪里如何挂钩,高达取决于你想要多少工作要做。您可以扩展DataGrid类并引入DependencyProperty“SingleClickEdit”或任何您想要的名称。然后,当监视器/预览键向下并在选项卡上选择单元格并强制它处于编辑模式。或者,如果你需要它只是该列,你可能只是监测:

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

然后在的.cs代码,在OnGotFocus()例如,呼叫datagrid.BeginEdit()。

编辑:(每下面的注释/ converation)

  • 添加的SelectionChanged处理程序到您的DataGrid
  • 中的.cs

    添加IsDropDownOpen =忠于你的组合框

    <DataGrid x:Name="dataGrid" 
         SelectionChanged="dataGrid_SelectionChanged" 
         ....> 
    
    <ComboBox Width="290" Name="cmbStock" ItemsSource="{Binding Path=Stocks}" 
         ... 
         IsDropDownOpen="True"></ComboBox> 
    </DataTemplate> 
    
  • private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
        dataGrid.BeginEdit(); 
    } 
    

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

+0

时,单元格应该变为可编辑状态 – 2012-04-23 18:20:45

+0

好吧,让我输入一些真正快速的测试内容,然后我会回复你asap – 2012-04-23 18:24:51

+0

感谢您的关注 – 2012-04-23 18:29:34