locked
Binding to ComboBox using Astoria v1, EF v1, and Silverlight 3 RRS feed

  • Question

  • I am using the following technologies: Silverlight v3, Astoria v1 and EF v1.   I am having a bit of difficulty showing the value through a ComboBox when the form loads, i.e. nothing is displaying.  But when I click on the drop-down arrow, I can see the list that it is bound and select values just fine.  

    I have a form via a UserControl that displays many properties (for Employee data like FirstName, LastName, Department).  The Department is actually stored as DepartmentID and when I hook up EF to it (i.e. create the conceptual model), a property called Departments is created for the class Employee.  Any advice will be greatly appreciated.

    Here is my XAML code:
    ---------------------------------------
    <UserControl.Resources>
            <DataTemplate
                x:Key="DepartmentTemplate">
                <StackPanel>
                    <TextBlock
                        Text="{Binding DepartmentName, Mode=OneWay}" />
                </StackPanel>
            </DataTemplate>
        </UserControl.Resources>
    
    <ComboBox x:Name="cboDepartment"
                        SelectedItem="{Binding Mode=TwoWay, Path=DEPARTMENTS}"
                        ItemTemplate="{StaticResource DepartmentTemplate}" />
    Here is my C# code:
    void OnControlLoaded(object sender, RoutedEventArgs e)
    {
       departments = new Departments(context);
       cboDepartment.ItemsSource = departments ;
       cboDepartment.SelectedItemChanged += cboDepartment_SelectedItemChanged;
       cboDepartment.GetData();
    }
    
    void cboDepartment_SelectedItemChanged(object sender, PropertyChangedEventArgs<object> e)
    {
       Employee emp= DataContext as Employee;
       Department dept = cboDepartment.SelectedItem as Department;
       context.SetLink(emp, "DEPARTMENTS", dept);
    }

    Friday, November 20, 2009 1:36 AM

All replies

  • I see two things, first you don't need to use the ItemTemplate, just set the DisplayMember path to "DepartmentName" to tell the control what to display. Second when the data loads, set the SelectItemIndex to get a value to show up in the combobox after the data loads.

    Shayne Burgess
    Program Manager
    Friday, February 12, 2010 1:06 AM
    Moderator