0

I have a DataGrid and I'm subscribing to PreparingCellForEdit. On it there is a code that returns the DataGridCell by getting the element from keyboard focus.

var cell = Keyboard.FocusedElement as DataGridCell;

When I have the cell I need to find out if there is a focusable element inside it that is enabled, visible and editable.

Example:

 FieldX  |      FieldY
--------------------------
   [ ]+  |       [ ]+
   [ ]*  |  (not visible)

+ Enabled, visible, focusable
* IsEnabled = false

How can I find out if there is such an element inside the cell?

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • See [this](http://stackoverflow.com/a/17000966/1997232). Why do you need that? Are you using [MVVM](http://stackoverflow.com/q/6358740/1997232)? – Sinatr Jul 03 '15 at 11:42
  • Yes and no, well, I don't have full freedom on this code I have to workaround it. Any major changes could impact on several other screens so I want to minimize the risk at this moment. I will have a look on that question, thanks. – BrunoLM Jul 03 '15 at 11:50

1 Answers1

0

use VisualTreeHelper to traverse through the child elements and then check if these elements fulfill your requirements. Something like this:

var elementList = new List<DependencyObject>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
{
    var element = VisualTreeHelper.GetChild(dependencyObject, i);
    if ((Visibility)element.GetValue(FrameworkElement.VisibilityProperty) == Visibility.Visible)
        elementList.Add(element);
}
Domysee
  • 12,718
  • 10
  • 53
  • 84