
I got the appropriate behaviour creating a attached behaviour which set the initial text and enabled/disabled the filter based on different events. However, there's still something that I haven't been able to solve.
I want the text in the AutoCompleteBox to be updated whenever SelectedItem is changed, not just when you do a selection from the AutoCompleteBox itself.
Below is a short sample. I have a AutoCompleteBox and a standard ComboBox both bound to the same object.
When I change my selection in the ComboBox, I want the text in the AutoCompleteBox to change also. By opening the dropdown on the AutoCompleteBox, you can see that the correct item is selected, but the text has remained unchanged.
This may not be how you intend the AutoCompleteBox to work, but it would be nice to control this through a property. I suspect that also would remove the need for me to set the initial text in the Attached Behaviour.
<Window x:Class="Autocomplete.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Autocomplete"
mc:Ignorable="d"
xmlns:editors="http://schemas.actiprosoftware.com/winfx/xaml/editors"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<editors:AutoCompleteBox x:Name="numbersBox" Width="300" Height="30" HorizontalAlignment="Left" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
PlaceholderText="Select a number">
<editors:AutoCompleteBox.DataFilter>
<editors:AutoCompleteBoxStringFilter />
</editors:AutoCompleteBox.DataFilter>
</editors:AutoCompleteBox>
<ComboBox x:Name="numbersComboBox" Grid.Column="1" Width="300" Height="30" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"></ComboBox>
</Grid>
</Window>
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using Autocomplete.Annotations;
namespace Autocomplete
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new MyViewModel();
vm.Items = new List<string> { "One", "Two", "Three", "Four" };
this.DataContext = vm;
vm.SelectedItem = vm.Items.Last();
}
public class MyViewModel : INotifyPropertyChanged
{
private string _selectedItem;
public string SelectedItem
{
get => _selectedItem;
set
{
if (value == _selectedItem) return;
_selectedItem = value;
OnPropertyChanged();
}
}
public List<string> Items { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}