開始學習C#/ XAML,很好,可能沒有做到這一點正確的方式。 我正在編寫一個程序來查看圖像,按他們的文件名列出它們,並且有一個用於搜索的文本框,當您鍵入它時會自動過濾列表。ICollectionView過濾器不會刷新 - 我的綁定是否錯誤?
我已經跑遍了我的C#代碼多次,我可以看到過濾器實際上工作 - 它看起來好像刷新也發射 - 但我不能直觀地得到它在屏幕上過濾或'刷新'。
的C#的盒子是如下
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列表。是的,這是正確的 - 無論我使用靜態過濾器,還是嘗試使用文本框內容更新的方式進行一番嘗試。然而在代碼中,過濾器似乎運行。 – deaddog