2012-05-16 43 views
0

有一個我無法解決的奇怪問題。我有一個GridView,它有一個按鈕來編輯用戶的角色列表,並將其顯示在子窗口中。該窗口具有綁定到ria查詢的列表框。第一次顯示子窗口時,一切正常。但是,不止一次打開它時,列表框的項目集合爲空,所以複選框設置不正確。爲什麼項目集合是空的?重新顯示子窗口時,項目集合爲空

XAML:

<ListBox Name="lstRoles" HorizontalAlignment="Left" Height="406" Margin="10,10,0,0" VerticalAlignment="Top" Width="372" Loaded="lstRoles_Loaded_1" ItemsSource="{Binding Data, ElementName=rolesQueryDomainDataSource, Mode=OneWay}" ScrollViewer.VerticalScrollBarVisibility="Visible"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal" Name="spRoles"> 
        <CheckBox x:Name="chkRoleSelected" Unchecked="chkRoleSelected_Unchecked_1" Checked="chkRoleSelected_Checked_1"/> 
        <TextBlock x:Name="txtRoleId" Visibility="Collapsed" Text="{Binding RoleId}"/> 
        <TextBlock Text="{Binding Description}"/> 
       </StackPanel> 
      </DataTemplate>     
     </ListBox.ItemTemplate> 
    </ListBox> 

代碼隱藏:

private void FillListBox(ListBox lb) 
    { 
     MembershipContext context = new MembershipContext(); 
     EntityQuery<aspnet_UsersInRoles> query = from c in context.GetAspnet_UsersInRolesQuery() where c.UserId == _crmContact.UserId select c; 
     var loadOp = context.Load(query); 
     loadOp.Completed += (opSender, eArgs) => 
     { 
      var op = opSender as LoadOperation; 
      List<aspnet_UsersInRoles> rowData = ((LoadOperation<aspnet_UsersInRoles>)op).Entities.ToList(); 
      var index = 0; 

      foreach (var item in lb.Items) 
      { 
       var role = item as RolesQuery; 
       var lbi = lb.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem; 
       CheckBox chk = FindDescendant<CheckBox>(lbi); 
       var x = (from r in rowData where r.RoleId == role.RoleId select r).FirstOrDefault(); 
       if (x != null) 
       { 
        chk.IsChecked = true; 
       } 
       index++; 
      } 
     }; 
    } 

回答

0

你可能有一個時間的問題。嘗試在DomainDataSource完成加載後調用FillListBox。

+0

排序的時間問題(並不是真的)。我從DomainDataSource中刪除了AutoLoad =「true」,並從構造函數中調用了Load,並修復了它。 – rocketbob