How to enable free text for ComboBox in UWP

I added a ComboBox in the UWP application that I am working on in Windows 10, and I cannot do this so that users can enter free text if the value they need on it does not match any of the available values ​​for selection.

How can i do this?

According to MSDN, the description for ComboBox.IsEditable :

Gets or sets a value that enables or disables text editing in the ComboBox text field.

It seems to be necessary. However, this only looks like WPF.

XAML doesn't seem to have a custom IsEditable property:

IsEditable in XAML

In the code behind, when I try to access this property, it shows that the property has only get and that it always returns false :

IsEditable in code behind

Please let me know how I can make this possible. Thanks!

+5
source share
3 answers

I have not tried it yet, but here is a ComboBox implementation with an editable text field on UWP.

ComboBox with editable text box

And, in principle, the Autosuggest option is completely different from the edited ComboBox - Autosuggest simply offers a candidate from the current list of elements, but does not agree to enter a new one. To implement it, you need to implement a user control like the above guy.

+2
source

If your UWP application is running on a device with at least the Anniversary Update version and you have installed the minimum version of the UWP platform, at least on the Anniversary Update (version 1607), you can use the Dependency IsTextSearchEnabled property, which, when set to True, will allow you go to value by typing.

The problem is that you must create an instance of this code instead of the XAML code using the ApiInformation class, which prevents your application from running on an old version of Windows 10 that does not recognize this property dependency. Details here

If the minimum target version of your application is a version of Creators Update, you can also execute this solution at runtime on XAML based on the API present. Conditional Xaml

Define your own namespace that matches the conditional method where you ask if the 3rd version of UniversalApiContract exists (the minimum for this dependency property).

 xmlns:contract3Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,3)" 

Then in your ComboBox you can do the following:

 <ComboBox contract3Present:IsTextSearchEnabled="True"/> 

This dependency property will be set only if the device on which the application is installed has the required version of the operating system!


Another solution, completely different from the one discussed here, would be Custom Control , where you could extend the ComboBox logic by implementing a text search for it. You can listen for keystrokes while the ComboBox received focus, and you can order a collection based on keystrokes.

0
source

I'm not 100% how this translates to your format, but have you looked at the DropDownStyle property ? You can set it using 1 of 3 different ComboBoxStyle Properties . I believe that you are looking for a DropDown or Simple style that is user editable.

 myComboBox.DropDownStyle = ComboBoxStyle.DropDown; myComboBox.DropDownStyle = ComboBoxStyle.Simple; 

Then you can get the value of "Text" using myComboBox.Text to capture the value entered by the user.

-1
source

Source: https://habr.com/ru/post/1275523/


All Articles