DataGrid中:帮助访问在CellEditingTemplate定义的控制 [英] DataGrid: Help accessing a control defined in a CellEditingTemplate

查看:1777
本文介绍了DataGrid中:帮助访问在CellEditingTemplate定义的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF的DataGrid有DataGridTemplateColumn。该DataGridTemplateColumn.CellEditingTemplate包含IsEditable组合框设置为真。在我RowEditEnding事​​件处理程序,我想读取组合框的Text属性 - 唯一的问题是,我不知道怎么做才能让Text属性的事件处理程序中检索ComboBox实例

有关参考,这是我的DataTemplate:

 <! -  ...  - >
  <我:DataGridTemplateColumn.CellEditingTemplate>
      <&DataTemplate的GT;
          <组合框IsEditable =真的ItemsSource ={绑定源= {StaticResource的ProductCategories}}的SelectedItem ={绑定路径=名称}的DisplayMemberPath =名称/>
      < / DataTemplate中>
  < /我:DataGridTemplateColumn.CellEditingTemplate>
  <! - ... - >

和我的code:

 私人无效productsDataGrid_RowEditEnding(对象发件人,DataGridRowEditEndingEventArgs E)
    {
        // UH-OH!在哪里可以找到ComboBox实例?
    }

我知道,我可以用e.Row目前受影响的行...也许该解决方案涉及到的东西用e.Row?
我试着从e.Row递归遍历可视树向下寻找组合框的实例,但没有骰子。我几乎肯定的是,解决方法很简单,但是,我relativley新的WPF一般。
任何建议将是多少AP preciated。

谢谢!


解决方案

这似乎进入CellEditingTemplate是DataGrid中的preparingCellForEdit事件时可用。你可以连线了一个弯就在DataGrid的事件,并做处理程序这样的事情让你的组合框

 私人无效_CounterGoalsGrid_ preparingCellForEdit(对象发件人,
    DataGrid的preparingCellForEditEventArgs E)
    {
        组合框editCombo =(e.EditingElement.FindName(<您的组合框名称>)作为组合框);
    }


  • 记住的名字在XAML组合框的

I am using the WPF DataGrid with a DataGridTemplateColumn. The DataGridTemplateColumn.CellEditingTemplate contains a ComboBox with IsEditable set to 'true'. In my RowEditEnding event handler, I'd like to read the Text property of that ComboBox - the only problem is that I don't know how to retrieve the ComboBox instance within the event handler in order to get to the Text property.

For reference, here's my DataTemplate:

  <!-- ... -->
  <my:DataGridTemplateColumn.CellEditingTemplate>
      <DataTemplate>
          <ComboBox IsEditable="True" ItemsSource="{Binding Source={StaticResource ProductCategories}}" SelectedItem="{Binding Path=Name}" DisplayMemberPath="Name" />
      </DataTemplate>
  </my:DataGridTemplateColumn.CellEditingTemplate>
  <!-- ... -->

And my code:

    private void productsDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        // UH-OH! Where do I find the ComboBox instance?
    }

I know that I can get to the current affected row using e.Row... Maybe the solution involves something using e.Row? I've tried walking the visual tree recursively from e.Row down looking for an instance of ComboBox, but no dice. I'm almost positive that the solution is simple, however, I'm relativley new to WPF in general. Any suggestions would be much appreciated.

Thanks!

解决方案

It seems access to the CellEditingTemplate is only available during the PreparingCellForEdit event in the DataGrid. You could wire up a hander for that event on the DataGrid and do something like this on the handler to get to your ComboBox

private void _CounterGoalsGrid_PreparingCellForEdit(object sender, 
    DataGridPreparingCellForEditEventArgs e) 
    {
        ComboBox editCombo = (e.EditingElement.FindName("<your combobox name>") as ComboBox);
    }

  • Remember to name your ComboBox in xaml.

这篇关于DataGrid中:帮助访问在CellEditingTemplate定义的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆