Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what I ended up with, a slight adaptation of Sevenate's answer, so if you wanted to upvote, do that to his post.</p> <p>I used a subclass (I had the <code>AutoCompleteBox</code> subclassed already for other reasons), which allows me to create a wrapper dependency property to get the readonly <code>SearchText</code> (=what the user entered via keyboard) to the ViewModel - instead of a blend behavior, which is a perfectly valid way, too.</p> <p>The crux of the matter is that you should only apply the dynamic sorting upon changes of SearchText, not <code>Text</code> (=what is displayed in the <code>AutoCompleteBox</code>, will also change if a suggestion is selected in the dropdown). Sevenate's way to raise the PropertyChanged event of the readonly ItemsSource (<code>ItemsSorted</code>) is a nice and clean way to apply the sorting.</p> <p>ViewModel:</p> <pre><code>public class Item { public string Name { get; set; } public override string ToString() { return Name; } } public class AutoCompleteBoxDynamicSortingVM : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private readonly ObservableCollection&lt;Item&gt; source; public AutoCompleteBoxDynamicSortingVM() { 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)"}, }; } public IEnumerable&lt;Item&gt; ItemsSorted { get { return string.IsNullOrEmpty(Text) ? (IEnumerable&lt;Item&gt;)source : source.OrderBy(item =&gt; item.Name.IndexOf(Text, StringComparison.OrdinalIgnoreCase)); } } public Item Selected { get; set; } // Text that is shown in AutoCompleteBox private string text; public string Text { get { return text; } set { text = value; OnPropertyChanged("Text"); } } // Text that was entered by user (cannot be changed from viewmodel) private string searchText; public string SearchText { get { return searchText; } set { searchText = value; OnPropertyChanged("SearchText"); OnPropertyChanged("ItemsSorted"); } } private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p>Subclass of AutoCompleteBox:</p> <pre><code>public class MyAutoCompleteBox : AutoCompleteBox { /// &lt;summary&gt; /// Bindable property that encapsulates the readonly property SearchText. /// When the viewmodel tries to set SearchText by way of EnteredText, it will fail without an exception. /// &lt;/summary&gt; public string EnteredText { get { return (string)GetValue(EnteredTextProperty); } set { SetValue(EnteredTextProperty, value); } } public static readonly DependencyProperty EnteredTextProperty = DependencyProperty.Register("EnteredText", typeof(string), typeof(MyAutoCompleteBox), new PropertyMetadata(null)); protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { // synchronize SearchText and EnteredText (only one-way) if (e.Property == AutoCompleteBox.SearchTextProperty &amp;&amp; this.EnteredText != this.SearchText) EnteredText = SearchText; base.OnPropertyChanged(e); } } </code></pre> <p>Xaml:</p> <pre><code>&lt;UserControl x:Class="WpfApplication1.Controls.AutoCompleteBoxDynamicSorting" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:myctrls="clr-namespace:WpfApplication1.Controls" xmlns:models="clr-namespace:WpfApplication1.ViewModels" Height="350" Width="525" DataContext="{DynamicResource viewModel}"&gt; &lt;UserControl.Resources&gt; &lt;models:AutoCompleteBoxDynamicSortingVM x:Key="viewModel" /&gt; &lt;DataTemplate DataType="{x:Type models:Item}"&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; &lt;Grid&gt; &lt;myctrls:MyAutoCompleteBox ItemsSource="{Binding ItemsSorted}" Text="{Binding Text, Mode=TwoWay}" EnteredText="{Binding SearchText, Mode=OneWayToSource}" FilterMode="ContainsOrdinal" VerticalAlignment="Top" Margin="5" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre>
    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.
 

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