2011-12-01 71 views
2

開始學習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; 
     } 
    } 
+0

你能否包含'AllImages'的定義?它是什麼類型?另外,我認爲當你啓動你的應用程序時,會顯示完整的圖像列表,但從未過濾? –

+0

@EsotericScreenName我已更新原始帖子以包含AllImages列表。是的,這是正確的 - 無論我使用靜態過濾器,還是嘗試使用文本框內容更新的方式進行一番嘗試。然而在代碼中,過濾器似乎運行。 – deaddog

回答

2

您的直覺正在進行中,問題來自您的綁定。您的ItemsSource未綁定到您的過濾器正在使用的相同視圖實例。試試這樣:

public ICollectionView CollectionView { get; set; } 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
     CollectionView = CollectionViewSource.GetDefaultView(AllImages); 
     new DynamicFiltering(CollectionView, this.TextBoxFilter); 
    } 

    <ListBox Name="VisualList" 
     DataContext="{Binding CollectionView}" 
     ItemsSource="{Binding}" 
     Width="Auto" 
     Grid.Row="1"/> 
+0

現身在隊友身上,非常感謝你。我試着最初將它綁定到'collectionView'實例,但是看起來我已經將原文稍微設置爲錯了。現在出色地工作。謝謝! – deaddog

1

而不是調用刷新,只需再次設置過濾器。

+0

與我對另一個答案的回答相掛鉤,我認爲問題不在於更新,而是錯誤的約束。但是我不能確定任何其他可能的ItemSource。 – deaddog

0

你的感覺是對的,問題在於列表框的綁定。刪除DataContext設置,然後設置ItemsSource="{Binding AllImages}"

+0

我試過了,它沒有解決任何問題 - 看起來沒有任何區別。我已經嘗試設置一個靜態過濾器沒有任何更新從文本框(只餵過濾器一個字符串),並再次,當我通過我的代碼時,它確實似乎設置過濾器 - 因爲它返回假和真取決於它是否匹配過濾器criterea - 但顯示的結果不反映任何過濾。 我覺得這是再次更明顯的東西,就像我根本沒有正確的綁定,或者我沒有設置CollectionViewSource的權利或其他東西。 – deaddog

相關問題