Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have no idea how @user1089031 done this, but here is working sample for anyone who could be interested in (<strong>updated to @adabyron's comment!</strong>):</p> <p><strong>ViewModel.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows.Data; namespace WpfApplication12 { public class Item { public string Name { get; set; } public override string ToString() { return Name; } } public class ViewModel: INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate {}; private readonly ObservableCollection&lt;Item&gt; source; private readonly ICollectionView items; private string searchText; public ViewModel() { source = new ObservableCollection&lt;Item&gt; { new Item {Name = "111111111 Test abb - (1)"}, new Item {Name = "22222 Test - (2)"}, new Item {Name = "333 Test - (3)"}, new Item {Name = "44444 Test abc - (4)"}, new Item {Name = "555555 Test cde - (5)"}, new Item {Name = "66 Test - bbcd (6)"}, new Item {Name = "7 Test - cd (7)"}, new Item {Name = "Test - ab (8)"}, }; items = new ListCollectionView(source); } public ICollectionView Items { get { return items; } } public IEnumerable&lt;Item&gt; ItemsSorted { get { return string.IsNullOrEmpty(SearchText) ? source : (IEnumerable&lt;Item&gt;)source .OrderBy(item =&gt; item.Name.IndexOf(SearchText, StringComparison.InvariantCultureIgnoreCase)); } } public Item Selected { get; set; } public string SearchText { get { return searchText; } set { searchText = value; PropertyChanged(this, new PropertyChangedEventArgs("SearchText")); PropertyChanged(this, new PropertyChangedEventArgs("ItemsSorted")); } } } } </code></pre> <p><strong>MainWindow.xaml</strong>:</p> <pre><code>&lt;Window x:Class="WpfApplication12.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" xmlns:wpfApplication2="clr-namespace:WpfApplication12" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" Title="MainWindow" Height="200" Width="500" DataContext="{DynamicResource viewModel}"&gt; &lt;Window.Resources&gt; &lt;wpfApplication2:ViewModel x:Key="viewModel" /&gt; &lt;DataTemplate DataType="{x:Type wpfApplication2:Item}"&gt; &lt;TextBlock Text="{Binding Name}" FontFamily="Courier New" /&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;controls:AutoCompleteBox ItemsSource="{Binding ItemsSorted}" FilterMode="ContainsOrdinal" SelectedItem="{Binding Selected, Mode=TwoWay}" MinimumPrefixLength="0" VerticalAlignment="Top" Margin="5"&gt; &lt;i:Interaction.Behaviors&gt; &lt;wpfApplication2:SearchTextBindBehavior BoundSearchText="{Binding SearchText, Mode=OneWayToSource}" /&gt; &lt;/i:Interaction.Behaviors&gt; &lt;/controls:AutoCompleteBox&gt; &lt;ListBox Grid.Column="1" ItemsSource="{Binding Items}" Margin="5" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>As you could notice I've add one custom behavior to <code>AutoCompleteBox</code> control:</p> <pre><code>&lt;i:Interaction.Behaviors&gt; &lt;wpfApplication2:SearchTextBindBehavior BoundSearchText="{Binding SearchText, Mode=OneWayToSource}" /&gt; &lt;/i:Interaction.Behaviors&gt; </code></pre> <p>This is because <code>AutoCompleteBox</code>'s own <code>SearchText</code> property is read-only. So here is the code of this behavior:</p> <p><strong>SearchTextBindBehavior.cs</strong> (<strong>Updated</strong>)</p> <pre><code>using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; namespace WpfApplication12 { public class SearchTextBindBehavior : Behavior&lt;AutoCompleteBox&gt; { public static readonly DependencyProperty BoundSearchTextProperty = DependencyProperty.Register("BoundSearchText", typeof(string), typeof(SearchTextBindBehavior)); public string BoundSearchText { get { return (string)GetValue(BoundSearchTextProperty); } set { SetValue(BoundSearchTextProperty, value); } } protected override void OnAttached() { base.OnAttached(); AssociatedObject.TextChanged += OnTextChanged; } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.TextChanged -= OnTextChanged; } private void OnTextChanged(object sender, RoutedEventArgs args) { if(AssociatedObject.Text.Length == 0) { BoundSearchText = string.Empty; return; } if(AssociatedObject.SearchText == AssociatedObject.Text.Substring(0, AssociatedObject.Text.Length - 1)) { BoundSearchText = AssociatedObject.Text; } } } } </code></pre> <p>Note: To make it all work you will need to add reference to the <strong>System.Windows.Interactivity.dll</strong> from the <a href="http://www.microsoft.com/en-us/download/details.aspx?id=10801" rel="nofollow">Expression Blend 4 SDK</a>. This is just where <code>Behavior&lt;T&gt;</code> and a few its friends live.</p> <p>If you have Expression Blend already installed, you already have all the SDK and there is no need to download anything. Just in case - on my machine the assembly located here: </p> <p>C:\Program Files\Microsoft SDKs\Expression\Blend.NETFramework\v4.0\Libraries\System.Windows.Interactivity.dll</p> <p>And, finally, if you have some good reason to do NOT add reference to this popular official library, feel free to re-implemented this custom behavior in "the old way" via plain old attached properties.</p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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