2012-06-22 43 views
2

在MVVM Silverlight應用程序中,用戶可以在TextBox中輸入文本,並且ListBox的內容相應地發生變化。 例如:如果用戶輸入「TV」,則列表框將填充所有可用的電視品牌,並且用戶可以從列表框和列表框條目中選擇產品;接下來如果他進入「電腦」列表框內容改變並填充ComputerNames。ListBox MVVM中的ClearSelection

只要用戶鍵入內容,它就會在字典中使用匹配鍵的值進行搜索。

查看:

<TextBox Name="SearchTBox" Text="{Binding SearchStr,UpdateSourceTrigger=PropertyChanged}" /> 
<ListBox Name="AnalysisLBox" ItemsSource="{Binding DataList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
     SelectedItem="{Binding UserSelectedItem,Mode=TwoWay}"         
     Width="250" BorderBrush="Transparent" SelectedIndex="{Binding LBoxSelectedIndex,Mode=TwoWay}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

視圖模型:

SortedDictionary Data() 
{ 
    List<string> tvList = new List<string>() { "Sony", "SamSung", "LG","Sharp" }; 
    List<string> computerList = new List<string>() { "HP","Dell","Lenovo","Gateway" }; 
    List<string> cameraList = new List<string>() { "Nikon","Sony","Panasonic" }; 
    SortedDictionary<string, List<string>> Data = new SortedDictionary<string, List<string>>(); 
    Data.Add("TV", billingList); 
    Data.Add("Computer", salesOutList); 
    Data.Add("Camera", customerAllocationList); 
} 

ObservableCollection<string> dataList = new ObservableCollection<string>(); 
public ObservableCollection<string> DataList 
{ 
    get { return dataList ; } 
    set { dataList = value; NotifyPropertyChanged("DataList"); } 
} 

int lBoxSelectedIndex; 
public int LBoxSelectedIndex 
{ 
    get { return lBoxSelectedIndex; } 
    set { lBoxSelectedIndex = value; NotifyPropertyChanged("LBoxSelectedIndex"); } 
} 

string userSelectedItem; 
public string UserSelectedItem 
{ 
    get { return userSelectedItem; } 
    set 
    { 
     userSelectedItem = value; 
    dataList.Clear(); 
     LBoxSelectedIndex =-1; 
     NotifyPropertyChanged("UserSelectedItem"); 
    } 
} 

只要一個密鑰匹配用戶輸入的字符串( 'TV'),它填充的ObservableCollection<string> DataList控件與tvList這勢必到列表框。用戶鍵入Camera,清除dataList並添加cameraList。問題發生在這裏。當清除數據並填充新數據時,listBox的選擇不會被清除。先前選定位置的相同商品仍保持選中狀態。 我試圖將SelectedIndex從ViewModel的UserSelectedItem屬性設置爲-1,但它不起作用。

+0

設置userSelectedItem=null我不明白這一點,你要清除列表框列表框中選擇更改時? –

+0

用戶從列表框中選擇一個項目後立即清除列表框。 – Prathibha

回答

1

您還可以在

ObservableCollection<string> dataList = new ObservableCollection<string>(); 
public ObservableCollection<string> DataList 
{ 
    get { return dataList ; } 
    set 
    { 
     dataList = value; 
     userSelectedItem=null 
     LBoxSelectedIndex = -1; 
     NotifyPropertyChanged("DataList"); 
    } 
} 
+0

感謝user1002386 – Prathibha

2

我認爲你可能有你的財產困惑。當在列表框中進行選擇時,UserSelectedItem設置器被觸發並清除dataList並將LBoxSelectedIndex設置爲-1。所以當用戶從ListBox中選擇一個項目時會發生什麼,ListBox被清除並且沒有被選中。

相反,您應該在DataList更改時清除選擇。

string userSelectedItem; 
public string UserSelectedItem 
{ 
    get { return userSelectedItem; } 
    set 
    { 
     userSelectedItem = value; 
     NotifyPropertyChanged("UserSelectedItem"); 
    } 
} 

ObservableCollection<string> dataList = new ObservableCollection<string>(); 
public ObservableCollection<string> DataList 
{ 
    get { return dataList ; } 
    set 
    { 
     dataList = value; 
     LBoxSelectedIndex = -1; 
     NotifyPropertyChanged("DataList"); 
    } 
} 

您還需要清除DataListSearchStr被更新,它不等於任何在排序的字典中的密鑰。

string searchStr; 
public string SearchStr 
{ 
    get { return searchStr; } 
    set 
    { 
     searchStr = value; 
     LBoxSelectedIndex = -1; 
     if (string.IsNullOrEmpty(searchStr)) 
      DataList = null; 
     else 
     { 
      List<string> selectedValue; 
      if (Data.TryGetValue(searchStr, out selectedValue)) 
       DataList = new ObservableCollection<string>(selectedValue); 
      else 
       DataList = null; 
     } 
     NotifyPropertyChanged("SearchStr"); 
    } 
} 
相關問題