2011-02-02 30 views
5

這不是一個問題,但我的答案是我無法在互聯網上找到解決方案的問題。AutoCompleteBox和SearchText清除

我在清理MVVM Silverlight應用程序中的SearchText時遇到問題。我可以清除SelectedItem和Text,但SearchText被留下。它是隻讀的,不能通過綁定進行更改。

示例:具有國家/地區列表的AutoCompleteBox。當用戶想要進入澳大利亞時,他們輸入「au」,此時奧地利和澳大利亞就會列出該列表。用戶然後可以選擇澳大利亞並繼續。在編輯結束時,他們點擊一個'保存'按鈕。此時,您可能想要清除輸入新數據的數據。

即使您已經綁定到SelectedItem和Text屬性,並且分別將它們設置爲'null'和string.Empty,SearchText屬性仍然存在,並且AutoCompleteBox不會清除,但將包含'au'。

回答

2

我在互聯網上發佈了這些內容,但對控制本身沒有任何答案,所以我從另一個角度來看它,這可能會幫助最終像我一樣感到沮喪的人。

我正在使用Silverlight導航模板應用程序,該應用程序使用加載Silverlight頁面的NavigationFrame。我注意到,如果我導航到另一個頁面並返回到我的數據表單,SearchText被清除。綁定到屬性的任何值都保持有效,只有SearchText已在所有AutoCompleteBox上清除。因此,我使用了將NavigationFrame注入ViewModel的PageConductor方法,我可以調用刷新方法。我從Silverlight Firestarter事件的John Papa的example中得到了這個方法,我只是向IPageConductor接口添加了一個Refresh方法,所以現在我可以調用'PageConductor.Refresh()',就像重新加載頁面一樣。我希望這可以幫助那裏的人。

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

試試這個please.it工作對我來說

0

變種T = ProductCombo.ItemsSource; ProductCombo.ItemsSource = null; ProductCombo.Text = string.Empty; ProductCombo.SelectedValue = null; //ProductCombo.Text = string.Empty; ProductCombo.ItemsSource = t;

不幸的是,這是代碼隱藏,我需要一個MVVM解決方案。

1

您必須清除bindeaded到文本的SelectedItem綁定屬性的內部設置部分,像這樣的屬性:

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); 
     } 
    } 

所以,當你設置爲null屬性的SelectedItem,其他屬性將設置爲「」。

0

我最近有與我的WPF應用程序相同的問題。我發現解決方案不是將綁定到SelectedItem的對象設置爲null,而是將其設置爲默認值。花了我一段時間來弄清楚這一點。所以在你的例子中,它不是SelectedCountry = null,而是SelectedCountry = new SelectedCountry()。在這種情況下,SearchText也被清除。檢查我的SO帖子關於此事:Autocompletebox doesn't clear keyboard strokes

-1

當然SEARCHTEXT屬性是隻讀的,但我們可以得到AutoCompleteBox的子組件:

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

現在我們可以通過文本框的組件的Text屬性重置SEARCHTEXT:

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

在C#6.0中它更簡潔:

autoCompBox.GetChildByType<TextBox>(item => item.Name == "Text")?.Text = string.Empty; 
+0

一個解釋與代碼片段通常幫助。 – hardillb 2016-06-02 14:22:34

1

我發現的最簡單的方法是擴展AutoCompleteBox:

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

現在在您的XAML中使用新的AutoCompleteBoxClear控件。 (即用戶點擊數據形加)

這將清除只有當autocompletebox DataContext的變化空文本

注:我覺得DataContextChanged僅只是在Silverlight 5可用的,但我猜想,現在還有人使用Silverlight這些天將可能已經升級到現在...