用户界面未更新INotifyPropertyChanged [英] UI not being updated INotifyPropertyChanged

查看:90
本文介绍了用户界面未更新INotifyPropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到ListBox的类:

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代码:

<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>

发生更新的代码背后:

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

附加的Converter接受ObservableCollection并将其Count作为字符串返回.

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

应用程序正常运行,并且值正在更新,但更改未反映在UI上.

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

但是,当我打电话给我时: lstFolders.Items.Refresh(); 用户界面得到更新.

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

我缺少什么/做错了什么?

What am I missing/doing wrong ??

我看了几个问题,例如: 使用INotifyPropertyChanged的WPF绑定不会更新 但未能解决此问题.

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

DataContext通过此方法分配:

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

推荐答案

将我的评论转换为答案:

Converting my comment into an answer:

尝试直接绑定到MatchingFiles.Count.

Try binding directly to MatchingFiles.Count.

这篇关于用户界面未更新INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆