Question

This is not a question but my answer to a problem I could not find a solution to on the internet.

I had a problem clearing the SearchText in an MVVM Silverlight application. I could clear clear the SelectedItem and Text but the SearchText was left behind. It is read only and cannot be changed by binding.

Example: AutoCompleteBox with a list of Countries. When the user wants to enter Australia they enter 'au' at this point the list appers with Austria and Australia. The user can then select Australia and move on. At the end of editing they click on a 'Save' button. At this point it is likely that you would want to clear the data forn for entering new data.

Even if you have bindings to the SelectedItem and the Text properties and you set them to 'null' and string.Empty respectively the SearchText property remains and the AutoCompleteBox will not clear but will contain 'au'.

Was it helpful?

Solution

I posted about this all over the internet but could get no answer on the control itself and so I came at it from a different angle which may help someone who ends up frustrated like me.

I am using a Silverlight Navigation template application which uses a NavigationFrame in which to load Silverlight pages. I noticed that if I navigated to another page and returned to my data form the SearchText was cleared. Any values that were bound to properties remained valid, just the SearchText had cleared on all AutoCompleteBoxes. I therefore used the PageConductor method of injecting the NavigationFrame into the ViewModel where I could call the refresh method. I got this method from John Papa's example from the Silverlight Firestarter event , I simply added a Refresh method to the IPageConductor interface so I am now able to call 'PageConductor.Refresh()' which is like reloading the page. I hope this helps someone out there.

OTHER TIPS

var t = ProductCombo.ItemsSource;
ProductCombo.ItemsSource = null;
ProductCombo.Text = string.Empty;
ProductCombo.SelectedValue = null;
//ProductCombo.Text = string.Empty;
ProductCombo.ItemsSource = t;

Try this please.it worked for me

You must clear the property bindeaded to Text inside set part of SelectedItem Binded property, like this:

    public string AnalisisText
    {
        get { return _analisisText; }

        set
        {
            if (_analisisText == value)
            {
                return;
            }

            _analisisText = value;

            RaisePropertyChanged(AnalisisTextPropertyName);
        }
    }

    public DatosAutoCompletaPedidosDetalleViewDTO AnalisisSelect
    {
        get { return _analisisSelect; }

        set
        {
            if (_analisisSelect == value)
            {
                return;
            }


            _analisisSelect = value;

            if (_analisisSelect == null) AnalisisText = "";

            RaisePropertyChanged(AnalisisSelectPropertyName);
        }
    }

So, when you set null to property SelectedItem , the other property will set to "".

The easiest way I've found is to extend the AutoCompleteBox:

public class AutoCompleteBoxClear : AutoCompleteBox
{
    public AutoCompleteBoxClear()
    {
        DataContextChanged += (o, e) =>
        {                
            if (SelectedItem == null)
                Text = string.Empty;
        };
    }
}

Now use your new AutoCompleteBoxClear control in your XAML.

This clears the text only when autocompletebox datacontext changes to null (ie the user clicks add in the dataform.)

Note: I think DataContextChanged is available only in Silverlight 5, but I'd guess that anyone still using Silverlight these days would likely have upgraded by now...

var t = ProductCombo.ItemsSource; ProductCombo.ItemsSource = null; ProductCombo.Text = string.Empty; ProductCombo.SelectedValue = null; //ProductCombo.Text = string.Empty; ProductCombo.ItemsSource = t;

Unfotunately this is code-behind and I needed an MVVM solution.

I recently had the same problem with my WPF app. I found out that the solution is not to set the object bound to SelectedItem to null, but to its default value. Took me a while to figure this out. So in your example, it would not be SelectedCountry = null, but SelectedCountry = new SelectedCountry(). In this case the SearchText is cleared also. Check my SO post regarding this matter: Autocompletebox doesn't clear keyboard strokes.

Sure SearchText property is read-only, but we can get the child component of AutoCompleteBox:

var searchText = autoCompBox.GetChildByType<TextBox>(item => item.Name == "Text");

And now we can reset SearchText via Text property of TextBox-component:

if (searchText != null) searchText.Text = string.Empty;

In C# 6.0 it is more laconically:

autoCompBox.GetChildByType<TextBox>(item => item.Name == "Text")?.Text = string.Empty;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top