Skip to main content

Microsoft Silverlight

Unanswered Question AutoCompletebox: show customer name after Datacontext is changedRSS Feed

(0)

kobruleht
kobruleht

Member

Member

161 points

582 Posts

AutoCompletebox: show customer name after Datacontext is changed

AutoCompleteBox is placed to Usercontrol and its ValueMemberPath is set to entity property:

            ValueMemberPath="CustomerName"

UserControl Datacontext is to entity new entity. AutoCompleteBox show empty customer name which is OK.

If other entity is loaded, UserControl DataContext property is assigned to this entity like in code below ChangeDataContext()  method.

However AutoCompleteBox still shows empty customer name. How to force AutoCompleteBox to show customer name from DataContext entity ?

Andrus.

    partial class Layout : UserControl
    {

public void ChangeDataContext() {

DataContext = Customer2; 

}

}

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: AutoCompletebox: show customer name after Datacontext is changed

Did you DataBind or set the ItemsSource of the AutoCompleteBox?

Yifung Lin [MSFT]

kobruleht
kobruleht

Member

Member

161 points

582 Posts

Re: AutoCompletebox: show customer name after Datacontext is changed

There is no databinding (I set id in SelectionChanged) and ItemsSource is not refreshed. Should I create dummy single-item list for ItemsSource or is there better way ?

Andrus.

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3464 points

383 Posts

Re: AutoCompletebox: show customer name after Datacontext is changed

Hi,

   You are trying to set the AutoCompleteBox's text when a datacontext is set,aren't you?  If that is the case, you can try to bind the text property to the datacontext.

   Here is how i do it:

<input:AutoCompleteBox x:Name="ab" Width="100" Height="30" Text="{Binding DataContext.Name,RelativeSource={RelativeSource Self}}"></input:AutoCompleteBox>  // MyDataContext's object type has a property name, and here i want to show the name

   I tested it ,every time i click i set a new dataContext source. The Text of the ACB changed accordingly.

Best Regards

 

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

kobruleht
kobruleht

Member

Member

161 points

582 Posts

Re: AutoCompletebox: show customer name after Datacontext is changed

I tried

Binding b = new Binding(col.BoundPropertyName);
b.ValidatesOnExceptions = true;

b.NotifyOnValidationError = true;
box.comboBox.SetBinding(AutoCompleteBox.TextProperty, b);

This makes form opening very slow compared to creatign dummy item:

                    box.comboBox.SelectedItem = new PickListEntity()
                    {
                        DisplayMember = (string)DynamicDataSource.Entity.GetValue(col.BoundPropertyName),
                        ValueMember = DynamicDataSource.Entity.GetValue(col.ValuePropertyName)
                    };

Andrus.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities