5

这不是一个问题,而是我对在互联网上找不到解决方案的问题的回答。

我在清除 MVVM Silverlight 应用程序中的 SearchText 时遇到问题。我可以清除 SelectedItem 和 Text 但 SearchText 被抛在后面。它是只读的,不能通过绑定来更改。

示例:带有国家列表的 AutoCompleteBox。当用户想进入澳大利亚时,他们在此时输入“au”,列表出现在奥地利和澳大利亚。然后用户可以选择澳大利亚并继续前进。在编辑结束时,他们单击“保存”按钮。此时,您可能希望清除数据格式以输入新数据。

即使您绑定了 SelectedItem 和 Text 属性并将它们分别设置为“null”和 string.Empty,SearchText 属性仍然存在,AutoCompleteBox 不会清除但将包含“au”。

4

7 回答 7

2

我在整个互联网上发布了有关此内容的信息,但无法获得有关控件本身的答案,因此我从不同的角度来处理它,这可能会帮助像我这样最终感到沮丧的人。

我正在使用 Silverlight 导航模板应用程序,该应用程序使用 NavigationFrame 在其中加载 Silverlight 页面。我注意到,如果我导航到另一个页面并返回到我的数据表单,SearchText 就会被清除。绑定到属性的任何值仍然有效,只是 SearchText 已清除所有 AutoCompleteBoxes。因此,我使用 PageConductor 方法将 NavigationFrame 注入到 ViewModel 中,我可以在其中调用 refresh 方法。我从 Silverlight Firestarter 事件的 John Papa 的示例中获得了这个方法,我只是在 IPageConductor 接口中添加了一个 Refresh 方法,所以我现在可以调用“PageConductor.Refresh()”,这就像重新加载页面一样。我希望这可以帮助那里的人。

于 2011-05-22T10:38:07.710 回答
1
var t = ProductCombo.ItemsSource;
ProductCombo.ItemsSource = null;
ProductCombo.Text = string.Empty;
ProductCombo.SelectedValue = null;
//ProductCombo.Text = string.Empty;
ProductCombo.ItemsSource = t;

请试试这个。它对我有用

于 2011-06-29T17:12:25.003 回答
1

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

于 2011-11-24T04:06:05.497 回答
1

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

于 2017-01-27T13:27:18.140 回答
0

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.

于 2011-07-03T17:40:04.687 回答
0

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.

于 2012-09-15T08:10:39.067 回答
-1

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;
于 2016-06-02T13:53:37.207 回答