我也有一个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" />
代码(视图模型)- WOEquipmentItems通过Web Service正确填充:
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");
}
}
解决方案非常简单。我添加了这个:在我的ViewModel中,我注册了这个命令,触发了一个简单执行MyQuerableCollection.MoveToLastPage()
的"AddingNewItem"函数。这迫使我的网格& &;页移动到最后一页,这意味着我选择的项目是可见的&不抛出错误。
编辑添加-当我的集合正好在10时,它也抛出了一个错误。所以我包含了一个检查ItemCount % 10 == 0。如果是,我删除列表中的第一项(Equipment.RemoveAt[0])。因为在设置新项之后,我运行了LoadData()函数,所以这不是问题。