2

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 を文字列として返します。

アプリケーションは正常に動作しており、値は更新されていますが、変更が UI に反映されていません。

しかし、私が呼び出すと: lstFolders.Items.Refresh(); UI が更新されます。

私は何が欠けている/間違っていますか??

INotifyPropertyChanged を使用した WPF バインディングは更新されません が、この問題を解決できませんでした。

編集:

DataContext は、次のメソッドによって割り当てられています。

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

2 に答える 2

0

新しい を作成する代わりに、ObservableCollectionそれを.Clear(). それはトリックを行う必要があります。

于 2012-12-31T15:47:38.027 に答える
0

私のコメントを回答に変換する:

MatchingFiles.Count に直接バインドしてみてください。

于 2013-01-05T17:21:38.390 に答える