How to clear box

Jul 21, 2010 at 5:18 PM

Hi

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

Thanks

Coordinator
Jul 22, 2010 at 12:48 PM
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 4: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.

Coordinator
Jul 23, 2010 at 5: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 9:56 AM
// This seems to clear the textbox value for the intellibox contol

var
 child = intelliboxControl.FindChildByType<TextBox>(); child.Text = "";
Jul 19, 2011 at 7: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"
	Grid.Row="2"
	Grid.Column="1"
	Width="160"
	DataProvider="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
		AncestorType={x:Type Page}},
		Path=LDJobResultsProvider}"
	DisplayedValueBinding="{Binding LabourDistributionID}"
	ExplicitlyIncludeColumns="True"
	Focusable="True"
	HideColumnHeaders="True"
	IsEnabled="{Binding ElementName=radioLDJob,
		Path=IsChecked}"
	MinimumPrefixLength="2"
	ResultsHeight="120"
	SelectAllOnFocus="True"
	SelectedItem="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
		AncestorType={x:Type Page}},
		Path=SelectedLD,
		Mode=TwoWay}">
	<ib:Intellibox.Columns>
		<ib:IntelliboxColumn Width="60"
			DisplayMemberBinding="{Binding LabourDistributionID}"
			ForProperty="LabourDistributionID"
			Header="Labour Dist. #" />
	</ib:Intellibox.Columns>
</ib:Intellibox>

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).

Coordinator
Jul 19, 2011 at 1:26 PM

Is SelectedLD firing a NotifyPropertyChange event?

Jul 26, 2011 at 9:37 PM

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


<auto:Intellibox ResultsHeight="280"
Name="txtOverrideSearch"
ExplicitlyIncludeColumns="False"
DisplayedValueBinding="{Binding FromLocID}"
SelectedValueBinding="{Binding FromLocID}"
DataProvider="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Page}}, Path=SearchAutoCompleteProviderProperty, NotifyOnSourceUpdated=True}"
Height="26"
VerticalAlignment="Top">
</auto:Intellibox>
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.

var
 child = txtOverrideSearch.FindChildByType<TextBox>(); child.Text = "";
Mar 14, 2012 at 2: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 = "";
}
Yay!