2012-05-16 5 views
0

해결할 수 없었던 기묘한 문제가 있습니다. 자식 창에 표시되는 사용자의 역할 목록을 편집하는 단추가있는 gridview가 있습니다. 이 창에는 리아 쿼리에 바인딩 된 목록 상자가 있습니다. childwindow가 처음 표시되면 모든 것이 잘 작동합니다. 그러나 두 번 이상 열면 목록 상자의 항목 모음이 비어 있으므로 확인란이 제대로 설정되지 않습니다. 아이템 컬렉션이 비어있는 이유는 무엇입니까?항목 childwindow를 다시 표시 할 때 아이템 컬렉션이 비어 있습니다.

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> 

Codebehind가 :

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

일종의 타이밍 문제 (실제로는 그렇지 않습니다). AutoLoad = "true"를 DomainDataSource에서 제거하고 생성자에서 Load를 호출하여 고정했습니다. – rocketbob