How to clear box

Jul 21, 2010 at 4:18 PM


How, programmatically, do I clear the text box? Surely, it should be easy but I've tried numerous things and none seem to work.


Jul 22, 2010 at 11:48 AM
The Textbox is bound to the source that you have given it. Clearing the bound value will clear it. You can think of the textbox as a regular textbox when it is not performing a search. Does that help?
Jul 23, 2010 at 3:53 PM

Sorry to appear (be?) dumb but how exactly - as far as I can see the TextBox is not exposed as a property. Do I have to put some sort of method in my SearchProvider class, alongside the DoSearch method or is there an easier method that I'm just not seeing. I've tried, for example, SearchBox.SelectedValue=null but this doesn't have any effect. Thanks.

Jul 23, 2010 at 4:54 PM
Did you set the binding for the control to an object? The entire purpose of the control is to allow you to search and then update the binding source with the selected value. What is your end goal when you search? To get the result and do what with it? The Textbox is not meant to be cleared unless your object sets the value to null and calls notifypropertychanged, which will force it to clear. Joe
Jul 17, 2011 at 8:56 AM
// This seems to clear the textbox value for the intellibox contol

 child = intelliboxControl.FindChildByType<TextBox>(); child.Text = "";
Jul 19, 2011 at 6:31 AM

Thanks for the tip bschurko but that doesn't seem to work for me, Intellibox does not expose a method by that name.

I have been frustrated to no end by this issue.

I bind and OBJECT to SelectedItem, and then some time later due to a process the object that SelectedItem is bound to becomes null and the user is expected to choose a new item. But the user is confused because the text displayed still reflects the previously SelectedItem, even though the SelectedItem binding worked perfectly. I've had a look at the code and I think it has something to do with the text being maintained from multiple angles (displaying the typed search text, then the highlighted result, then the selected result), and so the Text property doesn't use data binding. I can't figure out where I would fix this though.... *sigh*

Here is a snippet from my main in-house application:

<ib:Intellibox Name="dataLDJob2"
	DataProvider="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
		AncestorType={x:Type Page}},
	DisplayedValueBinding="{Binding LabourDistributionID}"
	IsEnabled="{Binding ElementName=radioLDJob,
	SelectedItem="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
		AncestorType={x:Type Page}},
		<ib:IntelliboxColumn Width="60"
			DisplayMemberBinding="{Binding LabourDistributionID}"
			Header="Labour Dist. #" />

And here is some code which demonstrates what I do to the object bound to SelectedItem:

private void ResetForm()
      NewLabour = new Labour();
      NewLabour.DateWorked = DateTime.Today;
      SelectedLD = null;
      // wish I could find a way to clear the Text from the control after SelectedItem is set to null :(
It may be worth noting, that breaking at the end of that method, the SelectedItem value is correctly set to null. In fact, the user can perform another search and everything works fine. The displayed Text is simply not updated to blank, when the SelectedItem/SelectedValue is set to (or actually, I could set it to anything else, and it would not update).

Jul 19, 2011 at 12:26 PM

Is SelectedLD firing a NotifyPropertyChange event?

Jul 26, 2011 at 8:37 PM

Are you telling me the FindChildByType<YourType>() does not appear for the control in your XAML?

<auto:Intellibox ResultsHeight="280"
DisplayedValueBinding="{Binding FromLocID}"
SelectedValueBinding="{Binding FromLocID}"
DataProvider="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Page}}, Path=SearchAutoCompleteProviderProperty, NotifyOnSourceUpdated=True}"
Perhaps you need to configure your control the same as mine. Try adding the AncestorType being the Page class, etc.

Then the FindChildByType should appear, as it clearly does for me. Allowing me to clear the textbox.

 child = txtOverrideSearch.FindChildByType<TextBox>(); child.Text = "";
Mar 14, 2012 at 1:45 AM

Hi joefeser, I did not implement SelectedLD as a DependencyProperty.

I did some digging around bschurko's suggestion. Although I could not find a method called FindChildByType in the .net4 assemblies, I did find some code to implement one (bschurko, are you using Telerik extensions? They provide this..)

private T FindChildByType<T>(DependencyObject parent) where T : DependencyObject
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (child is T)
            return child as T;
        DependencyObject grandchild = FindChildByType<T>(child);
        if (grandchild != null)
            return grandchild as T;
    return default(T);

This worked a treat.

private void ResetForm()
    NewLabour = new Labour();
    NewLabour.DateWorked = DateTime.Today;
    SelectedLD = null;
    var child = FindChildByType<TextBox>(dataJob2);
    child.Text = "";