0

有一个我无法解决的奇怪问题。我有一个 gridview,它有一个按钮来编辑用户的角色列表,该列表显示在子窗口中。该窗口有一个绑定到 ria 查询的列表框。第一次显示子窗口时一切正常。但是当多次打开它时,列表框的项目集合是空的,所以复选框设置不正确。为什么项目集合是空的?

xml:

<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++;
            }
        };
    }
4

1 回答 1

0

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

于 2012-05-17T06:25:10.047 回答