2009-06-02 121 views
0

我試圖實現更新搜索短語更改時更新TreeView的實時更新搜索欄,但我沒有完全設法讓它更新我想要的方式。WPF搜索框和數據綁定到樹視圖

所有項目都在應用程序啓動時顯示在樹視圖中(它現在只包含一個子級)。當我輸入文本框並調用PropertyChanged事件時,SearchPhrase屬性也會正確更新,但不會調用Items get。我的猜測是它與演示模型Items屬性有關。我對嗎?

這裏是我的XAML:

<Border BorderBrush="Black"> 
     <TextBox VerticalAlignment="Top" x:Name="phrase" Text="{ Binding  Path=SearchPhrase, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" Height="24" /> 
    </Border> 
    <TreeView Height="200" Background="Gainsboro" Name="list" ItemsSource="{ Binding Path=Items, Mode=OneWay, UpdateSourceTrigger=PropertyChanged }" ItemTemplate="{StaticResource dataTemplate}" /> 

這是我的演示模型:

public class ProjectListPM : BasePM 
{ 
    private List<AnalysisInfo> items; 
    private String searchPhrase; 

    /// <summary> 
    /// Gets or sets the search phrase. 
    /// </summary> 
    public String SearchPhrase { 
     get 
     { 
      return this.searchPhrase; 
     } 
     set 
     { 
      if (value != null) 
      { 
       this.searchPhrase = value; 
       FirePropertyChanged<ProjectListPM>(o => o.SearchPhrase); 
      } 
     } 
    } 

    /// <summary> 
    /// The list of analysises to display in the list. 
    /// </summary> 
    public List<AnalysisInfo> Items { 
     get 
     { 
      return 
       items.OrderByDescending(i => i.GetSearchRelevanceTo(SearchPhrase)).Where(
        i => i.GetSearchRelevanceTo(SearchPhrase) > 0).ToList(); 
     } 
     set 
     { 
      if (value != null) 
      { 
       this.items = value; 
       FirePropertyChanged<ProjectListPM>(o => o.Items); 
      } 
     } 
    } 

    public ProjectListPM() 
    { 
     this.items = new List<AnalysisInfo>(); 
     this.SearchPhrase = String.Empty; 
    } 
} 

public class BasePM : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    /// <summary> 
    /// Called when a property is changed.  
    /// </summary> 
    /// <typeparam name="T">Type</typeparam> 
    /// <param name="exp">Function</param> 
    protected void FirePropertyChanged<T>(Expression<Func<T, Object>> exp) 
    { 
     string propertyName; 
     if (exp.Body is UnaryExpression) 
      propertyName = ((MemberExpression)((UnaryExpression)exp.Body).Operand).Member.Name; 
     else 
      propertyName = ((MemberExpression)exp.Body).Member.Name; 
     if (PropertyChanged != null) 
     { 
      //Switch to UI thread 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

回答

1

它看起來像你想根據搜索短語過濾樹視圖的項目集合。

作爲一項緊迫的解決方案,您可以添加一行到SearchPhrase二傳手:

set 
{ 
    if (value != null) 
    { 
     this.searchPhrase = value; 
     FirePropertyChanged<ProjectListPM>(o => o.SearchPhrase); 
     FirePropertyChanged<ProjectListPM>(0 => o.Items); 
    } 
} 

這將通知用戶界面刷新項目,任何時候你設置的SearchPhrase。

如果您想採用不同的方法,可以從ItemsControl擴展BasePM,在構造函數中設置ItemsSource和Items.Filter屬性,並在設置SearchPhrase時調用Items.Refresh()。

+0

這正是我發現自己的解決方案。謝謝,雖然:) – atsjoo 2009-06-16 07:46:43

0

是您的List類一個WPF可以使用嗎?我建議使用System.Collections.ObjectModel.ObservableCollection,因爲它已經實現了與WPF兼容的列表更改事件。