2012-06-22 49 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"); 
    } 
} 
相关问题