0

我也有一个带有 Datapager 的 Datagrid/Dataform 设置。我的 DataPager.PageSize 以声明方式设置为 10。我的所有三个控件的 ItemsSource 都设置为 QueryableCollectionView。如果我的收藏少于 10 条记录,我可以毫无问题地向收藏添加新项目。我在我的 DataForm(新项目)中单击 + 号,填写表格,捕获 DP 上的 edit_ended 事件,然后毫无问题地保存到我的 SQL DB。

但是,当我有 10 多个项目并单击 + 号(新项目)时,应用程序会引发“元素已经是另一个元素的子元素”错误。我不确定这两个控件(Gridview 或 DataPager)中的哪一个导致了这个问题,我也不知道如何解决它!

代码(Xaml):

<toolkit:DataForm x:Name="dataForm" 
    Width="{Binding Width, ElementName=GV1}" 
    CurrentItem="{Binding SelectedWOEquipment}" 
    ItemsSource="{Binding WOEquipmentItems}"
    AutoEdit="False" 
    Tag="Equipment"
Header="Add/Update Equipment"
    AutoCommit="False" 
    Margin="0,0,0,10" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="EditEnded">
            <cmd:EventToCommand Command="{Binding SaveEquipmentCommand}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="DeletingItem">
            <cmd:EventToCommand Command="{Binding DeleteCommand}"
                CommandParameter="{Binding Tag, ElementName=dataForm}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <toolkit:DataForm.ReadOnlyTemplate>
        <DataTemplate>
            <StackPanel>
                <toolkit:DataField>
                    <TextBox Text="{Binding EquipmentCode}" />
                </toolkit:DataField>
                <toolkit:DataField Label="Equipment Description">
                    <TextBox Text="{Binding EquipmentDescription}" />
                </toolkit:DataField>
                <toolkit:DataField Label="Hours">
                    <TextBox IsEnabled="False" Text="{Binding Hours}" />
                </toolkit:DataField>
            </StackPanel>
        </DataTemplate>
    </toolkit:DataForm.ReadOnlyTemplate>
    <toolkit:DataForm.EditTemplate>
        <DataTemplate>
            <StackPanel>
                <toolkit:DataField Label="Equipment Codes">
                    <TextBox Text="{Binding EquipmentCode}" />
                </toolkit:DataField>
                <toolkit:DataField Label="Equipment Description">
                    <TextBox IsEnabled="False"
             Text="{Binding EquipmentDescription}" />
                </toolkit:DataField>
                <toolkit:DataField Label="Hours">
                    <TextBox Text="{Binding Hours, Mode=TwoWay}" />
                </toolkit:DataField>
            </StackPanel>
        </DataTemplate>
    </toolkit:DataForm.EditTemplate>
    <toolkit:DataForm.NewItemTemplate>
        <DataTemplate>
            <StackPanel>
                <toolkit:DataField Label="Equipment Codes">
                    <telerik:RadComboBox x:Name="cboEquipment"
            ItemsSource="{Binding DataSource.EquipmentList, Source={StaticResource DataContextProxy}}"  
                        SelectedValue="{Binding EquipmentCode, Mode=TwoWay}"
                        Margin="0 0 20 0">                                    
                        <telerik:RadComboBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel/>
                            </ItemsPanelTemplate>
                        </telerik:RadComboBox.ItemsPanel>
                    </telerik:RadComboBox>
                </toolkit:DataField>
                <toolkit:DataField Visibility="Collapsed">
                    <TextBox Text="{Binding EquipmentDescription,Mode=TwoWay}" />
                </toolkit:DataField>
                <toolkit:DataField Label="Hours">
                    <TextBox Text="{Binding Hours,Mode=TwoWay}" />
                </toolkit:DataField>
            </StackPanel>
        </DataTemplate>
    </toolkit:DataForm.NewItemTemplate>
 </toolkit:DataForm>
 <Grid>
     <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="*" />
     </Grid.RowDefinitions>
     <telerik:RadGridView ItemsSource="{Binding WOEquipmentItems}"
         x:Name="GV1"
         ShowGroupPanel="False"
         DataLoadMode="Asynchronous"
         HorizontalAlignment="Left"
         HeaderRowStyle="{StaticResource CSAgvHeaderRowStyle}"
         SelectedItem="{Binding SelectedWOEquipment, Mode=TwoWay}"
         AutoGenerateColumns="False" 
         IsReadOnly="True">
             <telerik:RadGridView.Columns>
                 <telerik:GridViewDataColumn DataMemberBinding="{Binding EquipmentCode}" HeaderCellStyle="{StaticResource CSAgvHeaderCellStyle}" Header="Equipment ID" Width="100"/>
                 <telerik:GridViewDataColumn DataMemberBinding="{Binding EquipmentDescription}" HeaderCellStyle="{StaticResource CSAgvHeaderCellStyle}" Header="Description" Width="300"/>
                 <telerik:GridViewDataColumn DataMemberBinding="{Binding Hours}" HeaderCellStyle="{StaticResource CSAgvHeaderCellStyle}" Header="Hours" Width="75"/>
             </telerik:RadGridView.Columns>
             <i:Interaction.Triggers>
                 <i:EventTrigger EventName="SelectedCellsChanged">
                     <cmd:EventToCommand Command="{Binding EditEquipmentCommand}"/>
                 </i:EventTrigger>
              </i:Interaction.Triggers>
          </telerik:RadGridView>
          <sdk:DataPager Grid.Row="1" Source="{Binding WOEquipmentItems}"  PageSize="10" />

代码 (Viewmodel) - 通过 Web 服务正确填充的 WOEquipmentItems:

private QueryableCollectionView _WOEquipmentItems;
private TSMVVM.Model.WOEquipment _selectedWOEquipment = new TSMVVM.Model.WOEquipment();
public QueryableCollectionView WOEquipmentItems
    {
        get { return _WOEquipmentItems; }
        set
        {
            _WOEquipmentItems = value;
            RaisePropertyChanged("WOEquipmentItems");
        }
    }
public TSMVVM.Model.WOEquipment SelectedWOEquipment
    {
        get 
        {
            if (_selectedWOEquipment != null)
            {
                if (_selectedWOEquipment.EquipmentDescription == null && _selectedWOEquipment.EquipmentCode != null)
                {
                    _selectedWOEquipment.EquipmentDescription = "";
                    _selectedWOEquipment.EquipmentDescription = EquipmentDescriptionList[EquipmentList.IndexOf(_selectedWOEquipment.EquipmentCode)];
                }
            }
            return _selectedWOEquipment; 
        }
        set
        {
            _selectedWOEquipment = value;
            RaisePropertyChanged("SelectedWOEquipment");
        }
    }
4

1 回答 1

0

解决方案结果非常简单。我添加了这个:在我的 ViewModel 中,我注册了这个命令,触发了一个简单地执行的“AddingNewItem”函数MyQuerableCollection.MoveToLastPage()。这迫使我的网格和寻呼机移动到最后一页,这意味着我选择的项目是可见的并且不会引发错误。

编辑添加 - 当我的收藏正好是 10 时,它也抛出了一个错误。所以我检查了 ItemCount % 10 == 0 是否。如果是,我删除了列表中的第一项(Equipment.RemoveAt[0])。因为,在设置新项目后,我运行了我的 LoadData() 函数,这不是问题。

于 2011-04-27T16:45:20.357 回答