UI not being updated INotifyPropertyChanged

蓝咒 提交于 2019-12-11 02:07:27

问题


I have a class that is bound to a ListBox:

class FolderFM : INotifyPropertyChanged
{
    public FolderFM(string path)
    {
        this.Folder = new DirectoryInfo(path);
        this.Name = Folder.Name;
    }

    private string name;
    private DirectoryInfo folder;
    private ObservableCollection<FileInfo> matchingFiles;

    ...

    public ObservableCollection<FileInfo> MatchingFiles 
    {
        get { return matchingFiles; }
        set
        {
            matchingFiles = value;
            FireWhenPropertyChanged("MatchingFiles");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void FireWhenPropertyChanged(string property)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(property, new PropertyChangedEventArgs(property));
        }
    }
}

XAML Code:

<ListBox Name="lstFolders" ItemsSource="{Binding}" Style="{StaticResource FolderBoxStyle}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition Width="25" />
                            </Grid.ColumnDefinitions>
                            <Label Content="{Binding Path=Name}" FontWeight="Bold" FontSize="13" />
                            <Button Content="{Binding Path=MatchingFiles, Converter={StaticResource lcc}}" Grid.Column="1" FontWeight="Bold" Foreground="Red" />
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Code-Behind where the update is taking place:

private void btnAnalyze_Click(object sender, RoutedEventArgs e)
    {
        List<FileInfo> remainingFiles = files;
        foreach (FolderFM currentFolder in folders)
        {
            currentFolder.MatchingFiles = new ObservableCollection<FileInfo>();
            string folderName = currentFolder.Folder.Name;
            string[] splitName = folderName.Split(' ');
            for (int i = 0; i < remainingFiles.Count; i++)
            {
                if (remainingFiles[i] == null)
                    continue;
                FileInfo fileName = remainingFiles[i];
                string searchedName = fileName.Name;
                matchScore = 0;
                int searchCount = 0;
                foreach (string part in splitName)
                {
                    if (part.Length < 3 || part == "the")
                        continue;
                    matchScore += searchedName.Contains(part) ? 1 : 0;
                    searchCount += 1;
                }
                if (matchScore == searchCount)
                {
                    string destination = System.IO.Path.Combine(currentFolder.Folder.FullName, fileName.Name);
                    if (File.Exists(destination))
                        continue;
                    Directory.Move(fileName.FullName, destination);
                    currentFolder.MatchingFiles.Add(remainingFiles[i]);
                    //lstFolders.Items.Refresh();
                    remainingFiles[i] = null;
                }
            }
        }
        populateFiles();
    }

The attached Converter accepts the ObservableCollection and returns it's Count as string.

The application is working properly and the values are being updated but the changes are not being reflected on the UI.

However when I call: lstFolders.Items.Refresh(); the UI gets updated.

What am I missing/doing wrong ??

I looked at a few questions like: WPF Binding with INotifyPropertyChanged does not update but haven't been able to solve this problem.

Edit:

The DataContext is being assigned by this method:

private void populateFolders()
    {
        folders = getFolders(selectedFolder.FullName);
        lstFolders.ItemsSource = folders;
    }

private List<FolderFM> getFolders(string path)
    {
        List<FolderFM> d = new List<FolderFM>();
        foreach (string folder in Directory.GetDirectories(path))
        {
            d.Add(new FolderFM(folder));
        }
        return d;
    }

回答1:


Converting my comment into an answer:

Try binding directly to MatchingFiles.Count.




回答2:


Instead of creating a new ObservableCollection, just replace that with a .Clear(). It should do the trick.



来源:https://stackoverflow.com/questions/14098015/ui-not-being-updated-inotifypropertychanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!