WPF列表框没有刷新

时间:2013-06-27 05:36:12

标签: wpf listbox

美好的一天,

我有一个WPF MVVM应用程序,允许用户将项目拖动到列表框中。这是第一次成功运作。但是,如果需要,我需要能够将更多项目拖动到列表框中。

        <!-- XAML FileSelectionView.xaml -->
        <ListBox x:Name="listboxFileGroups"
                 ItemsSource="{Binding ImportGroups, Mode=TwoWay}"
                 SelectedItem="{Binding Path=SelectedValue,
                                        Mode=TwoWay}"
                 Style="{StaticResource myListboxStyle}"
                 helper:FocusExtension.IsFocused="{Binding IsImportGroupsFocused}"
                 telerik:RadDragAndDropManager.AllowDrop="True"
                 telerik:RadDragAndDropManager.DropInfo="OnDropInfo"
                 telerik:RadDragAndDropManager.DropQuery="OnDropQuery" />

这是我的View Model(FileSelectionViewModel.cs),其中包含实现INotifyPropertyChanged的相关代码:

    // This is the data source of the top listbox
    private ObservableCollection<string> importGroups;
    public ObservableCollection<string> ImportGroups
    {
        get 
        {
            System.Diagnostics.Debug.WriteLine("Retrieving Top Level Import Groups...");
            importGroups = fileImportProcess.ImportGroups;
            return importGroups;
        }

        set
        {
            importGroups = value;
            fileImportProcess.ImportGroups = importGroups;
            System.Diagnostics.Debug.WriteLine("Initializing Top Level Import Groups...");
            OnPropertyChanged("ImportGroups");
        }
    }

FileSelectionViewModel.cs然后接受这些更改并将它们传播到名为FileImportProcess.cs的类中(这也实现了INotifyPropertyChanged),如下所示:

    // This is the data source for FileSelection
    private ObservableCollection<string> importGroups;
    public ObservableCollection<string> ImportGroups
    {
        get 
        {
            System.Diagnostics.Debug.WriteLine("Retrieving Lower Level Import Groups");
            return parsedFiles.Keys.ToList().ToObservableCollection<string>();
        }
        set
        {
            importGroups = value;
            System.Diagnostics.Debug.WriteLine("Initializing Lower Level Import Groups");
            foreach (var item in importGroups)
            {
                System.Diagnostics.Debug.WriteLine("==> " + item.ToString());
            }
            OnPropertyChanged("ImportGroups");
        }
    }

我遇到的问题是我第一次将三个文件拖到列表框中,一切正常。当我尝试将更多文件拖入列表框时,FileImportProcess.cs类会确认我选择的额外文件,但不会将事件传播回FileSelectionViewModel.cs。这是我的示例输出:

Initializing Lower Level Import Groups
==> Import Group 1
==> Import Group 2
==> Import Group 3

Retrieving Top Level Import Groups...
Retrieving Lower Level Import Groups
Retrieving Top Level Import Groups...
Retrieving Lower Level Import Groups
Retrieving Top Level Import Groups...
Retrieving Lower Level Import Groups

0 - C:\Dropbox\Dropbox-Logs\NSN_2013_02_20_eNodeB_Funktion_Load_M8000-10.csv
1 - C:\Dropbox\Dropbox-Logs\NSN_2013_02_20_ENODEB_FUNKTION_LOAD_M8018-22.csv
2 - C:\Dropbox\Dropbox-Logs\NSN_2013_02_20_ENB_Transport_Load_M8004-11.csv
3 - C:\Dropbox\Dropbox-Logs\NSN_2013_02_20_EUTRAN_RELATION_HO_M8015-20.csv
4 - C:\Dropbox\Dropbox-Logs\NSN_2013_02_20_EUTRANCELL_BEARER_M8007-13.csv
5 - C:\Dropbox\Dropbox-Logs\NSN_2013_02_20_EUTRANCELL_ENB_HO_M8009-15.csv
6 - C:\Dropbox\Dropbox-Logs\NSN_2013_02_20_EUTRANCELL_HO_M8021-24.csv

Initializing Lower Level Import Groups
==> Import Group 1
==> Import Group 2
==> Import Group 3
==> Import Group 4
==> Import Group 5
==> Import Group 6
==> Import Group 7

如您所见,第一次拖动文件时,ViewModel会检索ImportGroups。我第二次拖动其他文件,我收到输入文件,但列表框没有正确更新。

编辑***我没有在“输出”窗口中看到任何内容,表明数据绑定有任何问题***

有什么想法吗?

TIA,

COSON

1 个答案:

答案 0 :(得分:0)

尝试ItemsSource="{Binding ImportGroups, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"