我也有Datapager的Datagrid/Dataform設置。我的DataPager.PageSize被聲明性地設置爲10.所有三個控件的My 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服務正確填充:
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");
}
}
看看這篇文章,看看它是否能夠幫助你至少追蹤源代碼。 http://whydoidoit.com/2010/08/30/debug-xaml-element-is-already-the-child-of-another-element/ – 2011-04-26 21:03:02
謝謝日產。我可以添加(也許應該有)我的文章。我已經通過他的XAMLValidator運行了,並且不會失敗。 :(謝謝,雖然。 – 2011-04-26 22:57:35