Understanding how Text Property and SelectedItem work together.

Sep 11, 2015 at 1:24 AM
Edited Sep 11, 2015 at 1:25 AM
I have the following AutoCompleteTextBox binded to my VM.
<ExtControl:AutoCompleteTextBox MinWidth="200" x:Name="acb" Background="White"
                             Height="25"
                             Margin="2"
                             DisplayMember="NOMBRE"
                             Watermark="Nombre del Producto" 
                             SelectedItem="{Binding SelectedProducto, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                             IconPlacement="Left"
                             IconVisibility="Visible"
                             Provider="{Binding PosiblesProductos}" Grid.Column="1" Grid.Row="1">
                        <ExtControl:AutoCompleteTextBox.LoadingContent>
                            <TextBlock Text="Loading..."
                       Margin="5"
                       FontSize="14" />
                        </ExtControl:AutoCompleteTextBox.LoadingContent>
                        <ExtControl:AutoCompleteTextBox.Icon>
                            <Border Width="20"
                    Height="20">
                                <Image Source="/GESAL;component/Resources/Images/search_icon.png"
                       Height="16"
                       Width="16"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
                            </Border>
                        </ExtControl:AutoCompleteTextBox.Icon>
                        <ExtControl:AutoCompleteTextBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding NOMBRE}"></TextBlock>
                            </DataTemplate>
                        </ExtControl:AutoCompleteTextBox.ItemTemplate>
                    </ExtControl:AutoCompleteTextBox>
It works as intended, setting the SelectedItem if i select one item from the suggestion list.

The thing I,m having trouble understanding is how do i properly bind the Text property to my VM. I have tried the following
Text="{Binding NombreProducto, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
But the setter is never called.

I have worked around this setting the property NombreProducto to the same value as the filter parameter in the function that returns the suggestion list but then i cant validate the control.
public SuggestionProvider PosiblesProductos
        {
            get
            {
                return new SuggestionProvider(filter => {
                    var posiblesProductos = new List<PRODUCTO>();
                    if (!string.IsNullOrEmpty(filter) && filter.Length >= 3 && SelectedAlmacen_Grupo != null && SelectedAlmacen_Grupo.ID_ALMACEN_GRUPO != "")
                        posiblesProductos = new cProducto().Seleccionar(SelectedAlmacen_Grupo.ID_ALMACEN_GRUPO, "S").Where(w => w.NOMBRE.Contains(filter)).ToList();
                    Nombre = filter;
                    return posiblesProductos;
                });
            }
        }
If i where to only choose from the suggestion list i would have no problem, but since the user can create new objects any text that does not match an item in the collection is valid and the
SelectedItem should be allowed to be null.

I have this vague idea that since im binding the DisplayMember to the path "NOMBRE" of the SelectedItem i'm over writing the Text property but then im lost at how this two properties can work together. And how should i properly bind to my Text property in my VM.
Sep 11, 2015 at 6:23 AM
Edited Sep 11, 2015 at 6:30 AM
For me works when I set:
Text="{Binding selectedText, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
and of course my selectedText property is string. I think that isn't possible to set both text and selectedItem in one time, because they exclude together (if selectedItem is null, how You want to set name of PRODUCTO - You must to create new PRODUCTO).
It will work when Your "SelectedProducto" is string, then You can call in your "saveCommand":
if(string.IsNullorWhitespace(SelectedProducto))
    SelectedProducto=selectedText;
or maybe it will work when you call in saveCommand:
if(selectedItem == null)
{
SelectedProducto = new PRODUCTO { name = selectedText };
}
If Yours "SelectedItem is <PRODUCTO>, it meas that You have more fields in model (such as ID, and some others).
So I create UserControl, witch contains AutoCompleteBox and two more Hyperlinks ("New", "Edit"), with visibility depending on "SelectedItem == null or not" and when user wants to add new PRODUCTO, he should click "New" and in new window PRODUCTO should be created. After save it, suggestionPRovider should be refreshed.
This's how I do in my case and it works.
Marked as answer by chava_aa on 9/11/2015 at 9:35 AM
Sep 11, 2015 at 5:45 PM
Thanks for the fast answer andreas. I was thinking the same thing that both the Text and the SelectedItem cant be set at the same time.

I bounded the Text property as you stated and it works.

Also for further reference on how this two properties interact, the Text property stops "listening" for changes the moment you select an item from the suggestion list. That means that if you are writing in the textbox and select one item, the Text property will have the value that the Textbox had the moment before you selected the item, not the selected item text as one would expect.