开发者

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.Me开发者_StackOverflow社区nuItemService.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜