ListBox MVVM中的ClearSelection [英] ClearSelection in ListBox MVVM
问题描述
在 MVVM Silverlight 应用程序中,用户可以在 TextBox 中输入文本,ListBox 内容会相应更改.例如:如果用户输入电视",则列表框将填充所有可用的电视品牌,并且用户可以从列表框和列表框条目中选择产品;接下来,如果他输入计算机",则ListBox的内容将更改并使用ComputerNames填充.
In a MVVM Silverlight application the user can enter text in the TextBox and the ListBox contents changes accordingly. E.g.: If the user enter "TV" the ListBox will populate all the available Television brands and the user can select a product from the ListBox and ListBox entries; next if he enters "computer" ListBox contents change and populate with ComputerNames.
用户键入内容后,便会立即在字典中搜索与键值匹配的值.
As soon as the user types something it searches in a dictionary with values matching key.
查看:
<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>
ViewModel:
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'),它就会用绑定到 ListBox 的 tvList 填充 ObservableCollection
dataList.用户键入Camera,它将清除dataList并添加cameraList.问题出现在这里.清除数据并填充新数据时,不会清除listBox的选择.先前选择的位置处的同一项目保持选中状态.我试图从ViewModel的UserSelectedItem属性中将SelectedIndex设置为-1,但没有用.
As soon as a key matches user typed string ('TV') it populates an ObservableCollection<string>
dataList with tvList which is bound to the ListBox. The user types Camera, it clears the dataList and adds cameraList. The problem occurs here. The listBox's selection is not cleared when it clears data and populates with new data. The same item at the previously selected location remain selected.
I tried to set the SelectedIndex to -1 from the UserSelectedItem Property of the ViewModel but it didn't work.
推荐答案
您还可以在
ObservableCollection<string> dataList = new ObservableCollection<string>();
public ObservableCollection<string> DataList
{
get { return dataList ; }
set
{
dataList = value;
userSelectedItem=null
LBoxSelectedIndex = -1;
NotifyPropertyChanged("DataList");
}
}
这篇关于ListBox MVVM中的ClearSelection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!