我也有一个带有 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");
}
}