AutoCompleteBox - Display the text of SelectedItem instead of PlaceholderText when databinding

Editors for WPF Forum

Posted 4 years ago by Farris
Version: 19.1.0681
Avatar

Hello,

I am wanting to use the AutoCompleteBox, but am currently facing some issues as to how it behaves. 

My scenario is that I display a value in a TextBlock, but it is possible to click on this TextBlock to edit the value. Previously a standard ComboBox was shown at the location of the TextBlock, but now I've switched it out with the AutoCompleteBox.

When clicking to edit the item, I want the currently selected item (if there is one) to be displayed, not the PlaceholderText.

How may I achieve that? 

Thanks,

Farris

[Modified 4 years ago]

Comments (7)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Farris,

It should already show the selected item's text when the user changes the selection.  Or do you mean you are binding the AutoCompleteBox.SelectedItem property and that isn't initially showing the selected item's text?

Or if I'm misunderstanding, please make a new simple sample project that shows the issue you are facing and perhaps include some screenshot mockups that shows what you want, and send those to our support address, referencing this thread in your e-mail.  Be sure to remove the bin/obj folders from the ZIP you send and rename the .zip file extension so it doesn't get spam blocked.  Thanks!


Actipro Software Support

Posted 4 years ago by Farris
Avatar

Or do you mean you are binding the AutoCompleteBox.SelectedItem property and that isn't initially showing the selected item's text?

This is the case. If SelectedItem isn't null when databinding, I want to display this item in the AutoCompleteBox' textbox. When I open the dropdown on the AutoCompleteBox the correct item is selected, but the textbox is showing the PlaceholderText. I would want the textbox to initally show the SelectedItem's text.

[Modified 4 years ago]

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Farris,

I did some toying around with updating the AutoCompleteBox's text on SelectedItem property updates.  There are a couple problems with making the requested change, mainly because AutoCompleteBox is modeled after behaviors in popular apps and isn't meant to be a drop-in replacement for a ComboBox.

The problems I see here are:

1) The auto-complete box implementations that we've looked at in the Visual Studio and Office apps don't update the text displayed in the control as list items are selected.  Since the nested ListBox's SelectedItem is bound to the containing AutoCompleteBox.SelectedItem, doing any sort of up/down selection in the ListBox would force the AutoCompleteBox's text to update.  The implementations we've looked at always keep the text static until a ListBox item is clicked or Enter is pressed, both of which indicate intention of a commit.

2) Another side effect of having the selected item update the text is that the current text filters down what is in the ListBox.  So if you use our countries AutoCompleteBox example and you bind to Finland, it would update the text to say Finland.  This in turn would mean that when the user opened the dropdown to see the nested ListBox, there would only be Finland visible since the rest of the items would be filtered out.  I don't think that would be a desired behavior.

I'm not sure how best to proceed on our end here since I don't think having the behaviors in #1 / #2 above would be someone we'd want to add. 

Do you have any thoughts on the above?  You could always just set the AutoCompleteBox.Text property yourself to change the text in the control if you're ok with #2.


Actipro Software Support

Posted 4 years ago by Farris
Avatar

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));
            }
        }
    }
}
Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Did you see our reply to the related ticket [017-254EB9C7-0004] from a couple days ago?  We added an AutoCompleteBox.SelectedItemChanged property and gave some additional "behavior" code updates for your sample that get it working how I think you're wanting.


Actipro Software Support

Posted 4 years ago by Farris
Avatar

Hello!

No, I can't see that I've received any mail with that (last mail I received was from Monday). 

That sounds great. Will this be in the next release?

- Farris 

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

We had replied on Tuesday with a link to a preview build where the AutoCompleteBox.SelectedItemChanged event was added and updates to the "behavior" code.  If you never got the notification for that, maybe check your spam email.  Or you can probably click the link in previous email notifications for the ticket to see the ticket online and our response there.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.