【发布时间】:2012-01-10 07:07:07
【问题描述】:
我正在编写一个程序来查看图像,按文件名列出它们,并有一个用于搜索的文本框,它会在您输入时自动过滤列表。
我可以看到过滤器实际工作 - 看起来好像刷新也在触发 - 但是它没有在屏幕上“刷新”。
代码
public MainWindow()
{
InitializeComponent();
DataContext = this;
ICollectionView collectionView = CollectionViewSource.GetDefaultView(AllImages);
new DynamicFiltering(collectionView, this.TextBoxFilter);
}
public class DynamicFiltering
{
public DynamicFiltering(ICollectionView filteredView, TextBox textBox)
{
string filterText = "";
filteredView.Filter = delegate(object item)
{
MyImages textvalue = item as MyImages;
string textvaluestring = textvalue.Name as string;
if (textvaluestring.ToUpper().Contains(filterText.ToUpper()))
return true;
else
return false;
};
textBox.TextChanged += delegate
{
filterText = textBox.Text;
filteredView.Refresh();
};
}
}
xaml 是
<TextBox x:Name="TextBoxFilter"/>
<ListBox Name="VisualList"
Template="{StaticResource StandardListBox}"
DataContext="{Binding AllImages}"
ItemsSource="{Binding}"
Width="Auto"
Grid.Row="1"/>
是列表框绑定的问题吗?另一件可能值得注意的事情是我在列表中的每个对象中都保存了一个字符串和图像源。
编辑: 下面是我用来填充 AllImages 的代码 - 使用 Environment.SpecialFolder.MyPictures 作为示例。
public class MyImages
{
public ImageSource _image;
public string _name;
public MyImages(ImageSource image, string name)
{
_image = image;
_name = name;
}
public override string ToString()
{
return _name;
}
public ImageSource Image
{
get { return _image; }
}
public string Name
{
get { return _name; }
}
}
public List<MyImages> AllImages
{
get
{
List<MyImages> result = new List<MyImages>();
foreach (string filename in
System.IO.Directory.GetFiles(
Environment.GetFolderPath(
Environment.SpecialFolder.MyPictures)))
{
try
{
result.Add(
new MyImages(
new BitmapImage(
new Uri(filename)),
System.IO.Path.GetFileNameWithoutExtension(filename)));
}
catch { }
}
return result;
}
}
【问题讨论】:
-
您能否包含
AllImages的定义?它是什么类型的?另外,我假设当您启动应用程序时,会显示完整的图像列表,但从未过滤过? -
@EsotericScreenName 我已更新原始帖子以包含列表 AllImages。是的,这是正确的 - 无论我使用静态过滤器还是尝试使用更新文本框内容的过滤器来稍微幻想一下。然而,在代码中,过滤器似乎确实在运行。
标签: c# wpf xaml binding filter