在MVVM Silverlight应用程序中,用户可以在TextBox中输入文本,ListBox内容也会相应地更改。 例如:如果用户输入“TV”,ListBox将填充所有可用的电视品牌,用户可以从ListBox和ListBox条目中选择产品;接下来如果他输入“计算机”ListBox内容改变并填充ComputerNames。
一旦用户输入内容,它就会在字典中搜索值与键匹配。
查看:
<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')匹配,它就会用一个绑定到ListBox的tvList填充ObservableCollection<string>
dataList。用户键入Camera,它清除dataList并添加cameraList。问题出在这里。清除数据并填充新数据时,不会清除listBox的选择。先前所选位置的相同项目仍保持选中状态。
我试图从ViewModel的UserSelectedItem属性中将SelectedIndex设置为-1,但它不起作用。
答案 0 :(得分:2)
我认为你的财产可能会混乱。在ListBox中进行选择时,会触发UserSelectedItem
setter并清除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");
}
}
当DataList
更新时,您还需要清除SearchStr
,并且它不等于排序字典中的任何键。
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");
}
}
答案 1 :(得分:1)
您还可以在
中设置userSelectedItem=null
ObservableCollection<string> dataList = new ObservableCollection<string>();
public ObservableCollection<string> DataList
{
get { return dataList ; }
set
{
dataList = value;
userSelectedItem=null
LBoxSelectedIndex = -1;
NotifyPropertyChanged("DataList");
}
}