Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding and support for rich text formatting
    text
    copied!<p>Consider the following scenario:</p> <ul> <li><p>I have a <code>ListView</code> that is bound to an <code>ObservableCollection</code> using the <code>DataContext</code>:</p> <pre class="lang-xml prettyprint-override"><code>&lt;ListView ItemsSource="{Binding}"&gt; </code></pre> <p>The <code>class</code> containing the <code>string</code> data uses the <code>DependencyProperty</code> mechanism to keep the displayed content synced with the data collection.</p></li> <li>The <code>ListView</code> has one column that is editable (I followed the tutorial <a href="http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit" rel="nofollow">here</a> to achieve this); the <code>ListViewItem</code> is then either a <code>TextBlock</code> or a <code>TextBox</code>. This is done using a <code>DataTemplate</code> and two <code>Style</code> resources.</li> <li>I'd like to format the string displayed in the <code>TextBlock</code> based on a search string. Specifically, I'd like to format the items of the <code>ListView</code> to become bold as the user types in their search query if there is a match (only the characters that match in sequence should be made bold). This only needs to be displayed for the text currently being rendered using the <code>TextBlock</code> (that is, text not currently being edited).</li> </ul> <p>I've considered using an <code>IMultiValueConverter</code> that takes in a reference to the <code>TextBlock</code> that renders the data so that I can format the text appropriately. However, this will destroy the binding that I've set up:</p> <pre class="lang-xml prettyprint-override"><code>&lt;TextBlock.Text&gt; &lt;MultiBinding Converter="{StaticResource searchFormatter}" ConverterParameter="{x:Reference Name=txtSearch}"&gt; &lt;MultiBinding.Bindings&gt; &lt;Binding Path="NameOfBoundDependencyProperty"/&gt; &lt;Binding RelativeSource="{RelativeSource Self}"/&gt; &lt;/MultiBinding.Bindings&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; </code></pre> <p><code>searchFormatter</code> being the <code>IMultiValueConverter</code> and <code>txtSearch</code> being the <code>TextBox</code> containing the search query.</p> <p>I'm still learning WPF so I'm not familiar with the best approach or to what's possible. Is there a way to keep the data bound (so that edits reflect in the collection and the <code>ListView</code>) and still represent the data differently to the user (so that search matches may be bold)? Perhaps it would be cleaner if I manage the binding manually?</p>
 

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