Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF searchbox and databinding to treeview
    text
    copied!<p>I'm trying to implement a real-time updating search bar which updates a TreeView when search phrase changes, however I don't quite manage to get it to update the way I want.</p> <p>All the items are present in the treeview at application startup (it only contains one level of children at the moment). The SearchPhrase property is also updating correctly when I type in the textbox and the PropertyChanged event invoked, however the Items get is not invoked. My guess is that it has something to do with the presentation models Items property. Am I right?</p> <p>Here's my XAML:</p> <pre><code>&lt;Border BorderBrush="Black"&gt; &lt;TextBox VerticalAlignment="Top" x:Name="phrase" Text="{ Binding Path=SearchPhrase, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" Height="24" /&gt; &lt;/Border&gt; &lt;TreeView Height="200" Background="Gainsboro" Name="list" ItemsSource="{ Binding Path=Items, Mode=OneWay, UpdateSourceTrigger=PropertyChanged }" ItemTemplate="{StaticResource dataTemplate}" /&gt; </code></pre> <p>And here's my presentation model:</p> <pre><code>public class ProjectListPM : BasePM { private List&lt;AnalysisInfo&gt; items; private String searchPhrase; /// &lt;summary&gt; /// Gets or sets the search phrase. /// &lt;/summary&gt; public String SearchPhrase { get { return this.searchPhrase; } set { if (value != null) { this.searchPhrase = value; FirePropertyChanged&lt;ProjectListPM&gt;(o =&gt; o.SearchPhrase); } } } /// &lt;summary&gt; /// The list of analysises to display in the list. /// &lt;/summary&gt; public List&lt;AnalysisInfo&gt; Items { get { return items.OrderByDescending(i =&gt; i.GetSearchRelevanceTo(SearchPhrase)).Where( i =&gt; i.GetSearchRelevanceTo(SearchPhrase) &gt; 0).ToList(); } set { if (value != null) { this.items = value; FirePropertyChanged&lt;ProjectListPM&gt;(o =&gt; o.Items); } } } public ProjectListPM() { this.items = new List&lt;AnalysisInfo&gt;(); this.SearchPhrase = String.Empty; } } public class BasePM : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; /// &lt;summary&gt; /// Called when a property is changed. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Type&lt;/typeparam&gt; /// &lt;param name="exp"&gt;Function&lt;/param&gt; protected void FirePropertyChanged&lt;T&gt;(Expression&lt;Func&lt;T, Object&gt;&gt; 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)); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload