簡體   English   中英

在 Silverlight 4 中綁定到 DataGridColumn 中的 ComboBox

[英]Binding in Silverlight 4 to ComboBox in DataGridColumn

您好,我一直在到處尋找解決此問題的方法,但找不到解決方案。

我在 datagrid 列中有一個組合框,我想將它的 itemsource 綁定到由數據庫生成的列表。 我還想將選定的值綁定到單獨的表。 我已經成功地做到了……但只是有時。 有東西不同步。 這是一些代碼

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");
                }
            });


        }

有時,控件似乎在 getItemsAsync 完成之前綁定到列表。 有沒有辦法做到這一點,還是我應該放棄並嘗試其他方法?

謝謝

嘗試在您的 CategoriesProvider 上實現 INotifyPropertyChanged。 類別列表在哪里? 這就是您應該通知更改的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM