简体   繁体   中英

Binding in Silverlight 4 to ComboBox in DataGridColumn

Hello I have been searching everywhere for a solution to this problem and cannot figure out a solution.

I have a combo box in a datagrid column that I want to bind it's itemsource to a list generated by a database. I also want to bind the selected value to a separate table. I have succeeded in doing this... but only sometimes. There is something not synchronized. Here is some code

xaml:

<Grid.Resources>
    <my:CategoriesProvider x:Key="categoriesProvider"/>
</Grid.Resources>

..........................................

    <data:DataGridTemplateColumn Header="Category" Width="100" x:Name="cboDataCol">
                          <data:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate x:Name="cboDataTemplate">
   <ComboBox Name="cboCategories" SelectedItem="{Binding category, Mode=TwoWay}" ItemsSource="{Binding CategoriesList,Source={StaticResource categoriesProvider}}" DisplayMemberPath="name" SelectedValue="{Binding Path=category.id}"  SelectedValuePath="id"/>
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellEditingTemplate>
                    </data:DataGridTemplateColumn>

C#:

public class CategoriesProvider : List<category>
    {
        MenuItems.MenuItemService.MenuItemServiceClient svc = new     MenuItems.MenuItemService.MenuItemServiceClient();
        ObservableCollection<category> allCategories;

        public CategoriesProvider()
        {
            svc.getCategoriesCompleted += new EventHandler<getCategoriesCompletedEventArgs>(svc_getCategoriesCompleted);

            svc.getCategoriesAsync();
        }

        public void svc_getCategoriesCompleted(object sender, getCategoriesCompletedEventArgs e)
        {
            //m_autoresetevent.Set();
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                allCategories = e.Result;
                if (allCategories == null)
                {
                    MessageBox.Show("NULL123");
                }
            });


        }

Sometimes it seems that the control gets bound to the list before getItemsAsync is completed. Is there a solution to doing it this way, or should I give up and try something else?

Thanks

Try implementing INotifyPropertyChanged on your CategoriesProvider. Where is CategoriesList? That's what you should be notifying changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM