转换器停止过滤器工作

发布于 2024-09-12 06:04:45 字数 1958 浏览 1 评论 0原文

我试图在列表框中显示从特定目录检索的文件名。它们存储在 FileInfo 对象的 ObservableCollection 中:

public ObservableCollection<FileInfo> ProjectFiles
{
    get
    {
        if (SelectedFolder == null) return null;

        DirectoryInfo d= new DirectoryInfo(SelectedFolder);

        if (!d.Exists) return null;

        return new ObservableCollection<FileInfo>(d.EnumerateFiles("*.xsi"));
    }
}

我在列表框中实现了一个过滤器,在文本框“FilesFilterBy”中输入或更改文本时调用:

private void FilterFiles_TextChanged(object sender, TextChangedEventArgs e)
{
    ICollectionView view = CollectionViewSource.GetDefaultView(ProjectFiles);
    view.Filter = new Predicate<object>(IsTextInFilename);
}

public bool IsTextInFilename(object item)
{
    string Filename = Path.GetFileNameWithoutExtension((item as FileInfo).Name);
    return (Filename.ToLower().Contains(FilesFilterBy.Text.ToLower()));
}

同时,我想仅显示文件的名称,而不显示文件的名称。路径或扩展名。为此,我实现了一个转换器:

public class RemoveExtensionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    return Path.GetFileNameWithoutExtension(value as string);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return new NotImplementedException();
    }
}

以下是在 XAML 中实现列表框的方式:

<Window.Resources>
    <ctr:RemoveExtensionConverter x:Key="JustFileName" />
</Window.Resources>

<ListBox ItemsSource="{Binding ProjectFiles}" >
  <ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding FullName, Converter={StaticResource JustFileName}}" />
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

目前转换器可以工作 - 仅列出文件名,但过滤器不再有任何效果。当我在 FileFilterBy 文本框中输入文本时,将触发 TextChanged 事件,但列表框保持不变。此外,此时还没有调用转换器。

我做错了什么?

I am trying to display filenames in a listbox, retrieved from a particular directory. They are stored in an ObservableCollection of FileInfo objects:

public ObservableCollection<FileInfo> ProjectFiles
{
    get
    {
        if (SelectedFolder == null) return null;

        DirectoryInfo d= new DirectoryInfo(SelectedFolder);

        if (!d.Exists) return null;

        return new ObservableCollection<FileInfo>(d.EnumerateFiles("*.xsi"));
    }
}

I have implemented a filter on the listbox, called when text is entered or changed in a textbox "FilesFilterBy":

private void FilterFiles_TextChanged(object sender, TextChangedEventArgs e)
{
    ICollectionView view = CollectionViewSource.GetDefaultView(ProjectFiles);
    view.Filter = new Predicate<object>(IsTextInFilename);
}

public bool IsTextInFilename(object item)
{
    string Filename = Path.GetFileNameWithoutExtension((item as FileInfo).Name);
    return (Filename.ToLower().Contains(FilesFilterBy.Text.ToLower()));
}

At the same time, I want to display only the names of the files, without path or extension. To this end I have implemented a converter:

public class RemoveExtensionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    return Path.GetFileNameWithoutExtension(value as string);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return new NotImplementedException();
    }
}

Here is how the listbox is implemented in XAML:

<Window.Resources>
    <ctr:RemoveExtensionConverter x:Key="JustFileName" />
</Window.Resources>

<ListBox ItemsSource="{Binding ProjectFiles}" >
  <ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding FullName, Converter={StaticResource JustFileName}}" />
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

Currently the converter works - only the file names are listed, but the filter no longer has any effect. When I enter text in the FileFilterBy textbox the TextChanged event is fired but the listbox stays the same. Also, the converter is not called at that point.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2024-09-19 06:04:45

ProjectFiles 每次都会返回一个新集合。您的 FilterFiles_TextChanged 处理程序正在调用 ProjectFiles 创建一个新集合,在该新集合上设置过滤器,然后将其丢弃。绑定到 ListBox 的集合不受影响。您需要更改 ProjectFiles 以保留相同的集合对象。也许是这样的:

private ObservableCollection<FileInfo> _projectFiles;
public ObservableCollection<FileInfo> ProjectFiles
{
    get
    {
        if (_projectFiles == null)
        {
            if (SelectedFolder == null) return null;

            DirectoryInfo d = new DirectoryInfo(SelectedFolder);

            if (!d.Exists) return null;

            _projectFiles = new ObservableCollection<FileInfo>(
                d.EnumerateFiles("*.xsi"));
        }
        return _projectFiles;
    }
}

转换器根本不应该影响过滤器。

ProjectFiles returns a new collection every time. Your FilterFiles_TextChanged handler is calling ProjectFiles to create a new collection, setting a filter on that new collection, and then throwing it away. The collection bound to the ListBox is not affected. You need to change ProjectFiles to keep the same collection object. Maybe something like this:

private ObservableCollection<FileInfo> _projectFiles;
public ObservableCollection<FileInfo> ProjectFiles
{
    get
    {
        if (_projectFiles == null)
        {
            if (SelectedFolder == null) return null;

            DirectoryInfo d = new DirectoryInfo(SelectedFolder);

            if (!d.Exists) return null;

            _projectFiles = new ObservableCollection<FileInfo>(
                d.EnumerateFiles("*.xsi"));
        }
        return _projectFiles;
    }
}

The Converter shouldn't affect the filter at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文