3

在 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')ObservableCollection<string>匹配,它就会使用绑定到 ListBox 的 tvList 填充 dataList。用户键入 Camera,它会清除 dataList 并添加 cameraList。问题出现在这里。清除数据并填充新数据时,不会清除列表框的选择。先前选择的位置的相同项目保持选中状态。我试图从 ViewModel 的 UserSelectedItem 属性中将 SelectedIndex 设置为 -1,但它不起作用。

4

2 回答 2

2

你也可以设置 userSelectedItem=null

ObservableCollection<string> dataList = new ObservableCollection<string>();
public ObservableCollection<string> DataList
{
    get { return dataList ; }
    set 
    { 
        dataList = value;
        userSelectedItem=null
        LBoxSelectedIndex = -1;
        NotifyPropertyChanged("DataList"); 
    }
 }
于 2012-07-25T18:56:54.017 回答
2

I think you may have your properties confused. When a selection is made in the ListBox, the UserSelectedItem setter is triggered and clears the dataList and sets the LBoxSelectedIndex to -1. So really what happens when a user selects an item from the ListBox is the ListBox is cleared and nothing is selected.

Instead, it seems you should be clearing the selection when the DataList is changed.

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

You'll also need to clear the DataList when SearchStr is updated and it does not equal any of the keys in the the sorted dictionary.

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");
    }
}
于 2012-06-22T23:40:43.927 回答