How to get AutoCompleteBox to display text on initial load?

997 views Asked by At

The Autocompletebox works great except that when I initially load data, the text property is not updating. It works great otherwise.

Here is my xaml:

<Controls:AutoCompleteBox x:Name="CustomerNumber" Margin="5"
                        DockPanel.Dock="Top"
                        FilterMode="None" 
                        Populating="CustomerNumber_Populating"

                        Text="{Binding Model.SelectedFCust.CustId}"
                        SelectedItem="{Binding Model.SelectedFCust, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        ItemsSource="{Binding Model.FCusts}"
                        ValueMemberPath="CustId"
                        IsTextCompletionEnabled="True"
                        MaxDropDownHeight="110">
    <Controls:AutoCompleteBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding CustId}"/>
                <Run Text=" - "/>
                <Run Text="{Binding Name}"/>
            </TextBlock>
        </DataTemplate>
    </Controls:AutoCompleteBox.ItemTemplate>
</Controls:AutoCompleteBox>

I am setting Model.SelectedFCust to a valid value. But the autocompletebox shows no text in the UI. What could I be missing?

fyi, I also tried not having a Text binding, making the Text binding TwoWay, neither of these work either.

Here is the code where I set the initial value:

public void LoadId(int id)
{
    this.Forecast = _proxy.Load(id).ToReceptacle();
    this.FilterCustomer("");
    this.SelectedFCust = this.FCusts.FirstOrDefault(fc => fc.CustId == this.Forecast.CustomerNumber);
    this.IsDirty = false;
}

_proxy is an object that hooks into wcf services and brings in data from a database. A receptacle is just a class that represents the data in a way that amiable toward inotifypropertychanged logic.

My items source and selected item code...

public ObservableCollection<FilteredCustomer> FCusts
{
    get { return _FCusts; }
    set
    {
        if (_FCusts == value) return;

        _FCusts = value;
        OnPropertyChanged("FCusts");
    }
}
private ObservableCollection<FilteredCustomer> _FCusts;

public FilteredCustomer SelectedFCust
{
    get { return _SelectedFCust; }
    set
    {
        if (_SelectedFCust == value) return;

        _SelectedFCust = value;

        if (value != null)
            this.Forecast.CustomerNumber = value.CustId;
        else
            this.Forecast.CustomerNumber = "";

        OnPropertyChanged("SelectedFCust");
    }
}
private FilteredCustomer _SelectedFCust = null;    

UPDATE:

Through a bunch of trial and error I decided to use this code and it works. I just hook up the selectionchanged event.

private void CustomerNumber_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (vm.Model.IsDataLoaded && vm.Model.SelectedFCust != null)
        (sender as AutoCompleteBox).Text = vm.Model.SelectedFCust.CustId;
    else
        (sender as AutoCompleteBox).Text = "";
}

I am not satisfied with this solution whatsoever so the question is still open as to why the box isn't working properly.

I noticed that the text changed event is getting fired after the load occurs, but for the life of me cannot figure out what is changing it. There must be something wonky going on when the UI initializes.

0

There are 0 answers