0

Having a page and wanting to search for a control that is bound to a certain property I traverse the VisualTree. This is done on Page.Loaded event (based on this question). When traversing the Visual Tree I encounter the textbox I was looking for (in a testcase) but when using

var be = textbox.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);

be is null.

Does WPF still has to execute / render some visual controls and the bindings. And if so, which event could I hook onto to traverse the VisualTree with "activated" bindings?

btw: I already have it working for a sample project where I traverse the VisualTree on a button click. (page is loaded and bindings are fully functional then)

EDIT

XAML code:

<dxe:TextEdit Grid.Row="2" Grid.RowSpan="1" Grid.Column="5" Style="{DynamicResource {x:Static cbc:CustomStyle.QuestionTextEditStyle}}" Text="{Binding Path=Voorletters, Mode=TwoWay, ValidatesOnDataErrors=true}"/>

Code behind:

private void BasePage_Loaded(object sender, RoutedEventArgs e)
{
 if (!String.IsNullOrEmpty(_focusControlBindedToProperty))
 {
  this.FindVisualElementBindedTo(this, _focusControlBindedToProperty);
 }
}

private void FindVisualElementBindedTo(Visual visual, String propertyName)
{
    try
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
        {
            // Retrieve child visual at specified index value.
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(visual, i);

            if (childVisual is System.Windows.Controls.Primitives.TextBoxBase)
            {
                System.Windows.Controls.Primitives.TextBoxBase tbb = (System.Windows.Controls.Primitives.TextBoxBase)childVisual;

                BindingExpression be = tbb.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);

                if (be != null && be.ParentBinding != null && be.ParentBinding.Path.Path == propertyName)
                {
                    tbb.Focus();
                    tbb.SelectAll();

                    return;
                }
            }

            FindVisualElementBindedTo(childVisual, propertyName);
        }
    }
    catch { }
}
Community
  • 1
  • 1
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • Are you using WPF or Silverlight? They're similar but not the same thing. AFAIK there is no Page.Loaded in WPF. – CodingGorilla Jul 26 '11 at 13:40
  • Using WPF. And AFAIK there is a Page.Loaded event in WPF. MSDN states that "Standard data binding (binding to local sources, such as other properties or directly defined data sources) will have occurred prior to Loaded". I don't experience this promise :( – Youp Bernoulli Jul 26 '11 at 13:49
  • See if [DataContextChanged](http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontextchanged.aspx) works for you. – default.kramer Jul 26 '11 at 13:57
  • @default.kramer: this doesn't work for me – Youp Bernoulli Jul 26 '11 at 14:07

1 Answers1

0

Okay. Sorted out.

In the sample I created, I worked with standard Microsoft WPF controls (TextBox). So this line:

if (childVisual is System.Windows.Controls.Primitives.TextBoxBase)

was correct. Nevertheless, in the application to develop this functionality for we work with DevExpress WPF controls (TextEdit) which have a "total" different inheritance hierarchy. So the above if statement never succeeded.

Bindings are fully operational on page_load as Microsft stated: "Standard data binding (binding to local sources, such as other properties or directly defined data sources) will have occurred prior to Loaded"

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59