重新显示childwindow时,项目集合为空

时间:2012-05-16 20:12:25

标签: silverlight data-binding ria

有一个我无法解决的奇怪问题。我有一个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++;
            }
        };
    }

1 个答案:

答案 0 :(得分:0)

你可能有时间问题。在DomainDataSource完成加载后尝试调用FillListBox。