WPF AutoCompleteBox - How to limit it to selection only from a list of offers?

I would like to restrict WPF AutoCompleteBox (wpf toolkit control) to select an item only from a list of offers. It should not allow users to enter what they need.

Can someone tell me how to implement this? Any sample code is appreciated.

+5
source share
3 answers

Here is how I did it. Create a derived class and override OnPreviewTextInput. Set your collection to the ItemsSource control and it should work fine.

public class CurrencySelectorTextBox : AutoCompleteBox
{    
    protected override void OnPreviewTextInput(TextCompositionEventArgs e)
    {            
        var currencies = this.ItemsSource as IEnumerable<string>;
        if (currencies == null)
        {
            return;
        }

        if (!currencies.Any(x => x.StartsWith(this.Text + e.Text, true, CultureInfo.CurrentCulture))
        {
            e.Handled = true;
        }
        else
        {
            base.OnPreviewTextInput(e);
        }            
    }
}
+3
source

Priview. , ...

+1

, ,

<sdk:AutoCompleteBox ItemsSource="{Binding Sites, Source={StaticResource VmSchedulel}}" ValueMemberPath="SiteName"
                                             SelectedItem="{Binding Site, Mode=TwoWay}" FilterMode="ContainsOrdinal">
                            <sdk:AutoCompleteBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding SiteName}"/>
                                </DataTemplate>
                            </sdk:AutoCompleteBox.ItemTemplate>
                        </sdk:AutoCompleteBox>

If you enter any text that does not match anything in the ItemsSource element, SelectedItem will be zero. In the set method of your property, you can simply not set the value, because it is null, and the property will retain its original value.

 set
        {
            if (value != null)
            {
                BaseRecord.SiteID = value.ID;
                PropChanged("Site");
            }
        }
+1
source