使用 MVVM 将集合绑定到 WPF radgridview(childgrid) 中
本文关键字:radgridview childgrid WPF MVVM 集合 绑定 使用 | 更新日期: 2023-09-27 18:00:34
我正在研究Gridview
中添加,编辑,删除的功能。现在我有大约 4 个Collections
绑定到Gridview
及其孩子中。所有这些集合都是ObservableCollection
.绑定做得很好,但是当我尝试在主网格中添加一个新行时,其中还给出了子网格详细信息,集合的值将在新行中共享。
/XAML
<telerik:RadGridView x:Name="grd1" AutoGenerateColumns="False" Grid.Row="1" IsBusy="{Binding IsBusy, Mode=TwoWay, Source={StaticResource NPISViewModel}}" IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedNPIS, Mode=TwoWay}" RowDetailsVisibilityMode="{Binding VisibleMode, Mode=TwoWay, Source={StaticResource NPISViewModel}}" GridLinesVisibility="Both"
ItemsSource="{Binding NPISList, Mode=TwoWay, Source={StaticResource NPISViewModel}}" GroupRenderMode="Flat" ShowGroupPanel="False" >
<telerik:RadGridView.Columns>
<telerik:GridViewToggleRowDetailsColumn />
<telerik:GridViewDataColumn Header="Id" DataMemberBinding="{Binding Id}"/>
<telerik:GridViewImageColumn Header="Sketch" DataMemberBinding="{Binding Sketch}" Width="80*" IsFilterable="False" />
<telerik:GridViewDataColumn Header="RD #" DataMemberBinding="{Binding RDNumber}" Width="80*" />
</telerik:RadGridView.Columns>
<telerik:RadGridView.RowDetailsTemplate>
<DataTemplate>
<StackPanel>
<telerik:RadGridView x:Name="grdChild1" ItemsSource="{Binding NPISItemsList, Mode=TwoWay, Source={StaticResource NPISViewModel}}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedNPISItem, Mode=TwoWay, Source={StaticResource NPISViewModel}}"
HorizontalAlignment="Stretch"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:RadGridView}}, Path=ActualWidth, Converter={StaticResource PercentageConverter}, ConverterParameter=0.98}"
GridLinesVisibility="Both">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding Name}" Width="120*"/>
<telerik:GridViewDataColumn Header="Unit #" DataMemberBinding="{Binding Number}" Width="80*" />
<telerik:GridViewDataColumn Header="WidthIn" DataMemberBinding="{Binding WidthInches}" Width="40*" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
<telerik:RadGridView x:Name="grdChild2" ItemsSource="{Binding NPISCoversList, Mode=TwoWay, Source={StaticResource NPISViewModel}}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedNPISCover, Mode=TwoWay, Source={StaticResource NPISViewModel}}"
HorizontalAlignment="Stretch" Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:RadGridView}}, Path=ActualWidth, Converter={StaticResource PercentageConverter}, ConverterParameter=0.98}" GridLinesVisibility="Both">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="CoverName" DataMemberBinding="{Binding Name, Mode=TwoWay}" Width="120*" />
<telerik:GridViewDataColumn Header="ColorId" DataMemberBinding="{Binding ColorId, Mode=TwoWay}" Width="60*" />
<telerik:GridViewDataColumn Header="CleanCodeId" DataMemberBinding="{Binding CleaningInformationCode, Mode=TwoWay}" Width="60*" />
<telerik:RadGridView x:Name="grdChild3" ItemsSource="{Binding NPISNotesList, Mode=TwoWay, Source={StaticResource NPISViewModel}}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedNPISNote, Mode=TwoWay, Source={StaticResource NPISViewModel}}"
HorizontalAlignment="Stretch" GridLinesVisibility="Both" Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:RadGridView}}, Path=ActualWidth, Converter={StaticResource PercentageConverter}, ConverterParameter=0.98}" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Note" DataMemberBinding="{Binding Note, Mode=TwoWay}" Width="120*"/>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</StackPanel>
</DataTemplate>
</telerik:RadGridView.RowDetailsTemplate>
</telerik:RadGridView>
视图模型
/// <summary>
/// Get or set npis list.
/// </summary>
public ObservableCollection<NPISBO> NPISList
{
get { return m_NPISList; }
set
{
if (m_NPISList != value)
{
m_NPISList = value;
RaisePropertyChanged(() => NPISList);
}
}
}
/// <summary>
/// Get or set npis items list.
/// </summary>
public ObservableCollection<NPISItemBO> NPISItemsList
{
get { return m_NpisItemsList; }
set
{
if (m_NpisItemsList != value)
{
m_NpisItemsList = value;
RaisePropertyChanged(() => NPISItemsList);
}
}
}
/// <summary>
/// Get or set npis covers list.
/// </summary>
public ObservableCollection<NPISCoverBO> NPISCoversList
{
get { return m_NpisCoversList; }
set
{
if (m_NpisCoversList != value)
{
m_NpisCoversList = value;
RaisePropertyChanged(() => NPISCoversList);
}
}
}
/// <summary>
/// Get or set npis notes list.
/// </summary>
public ObservableCollection<NPISNoteBO> NPISNotesList
{
get { return m_NpisNotesList; }
set
{
if (m_NpisNotesList != value)
{
m_NpisNotesList = value;
RaisePropertyChanged(() => NPISNotesList);
}
}
}
NPISList = new ObservableCollection<NPISBO>(NPISModels.LoadNPIS(SelectedNPISId));
NPISCoversList = new ObservableCollection<NPISCoverBO>(NPISCoverModels.LoadAllNPISCovers());
NPISItemsList = new ObservableCollection<NPISItemBO>(NPISItemModels.LoadAllNPISItems());
NPISNotesList = new ObservableCollection<NPISNoteBO>(NPISNoteModels.LoadAllNPISNotes());
现在我只需要创建新行。我错在哪里?
注意:Telerik 版本 2013.1.403.45
当我搜索这个问题时,我得到了一些可能会有所帮助的观点。
-
ObservableCollection
缺乏关系(映射(,因此会出现此问题。 - 而不是网格中的
RowDetails
使用需要DataSet
但不应根据应用程序要求使用DataSet
ChildTableDefinition
。要么ObservableCollection
要么List<>
都很好。
我怀疑您的SelectedNPISItem
setter 没有正确填充子集合。但是,老实说,我会重构代码,以便 NPISItem 具有集合属性。可以将子网格绑定到项的属性,而不是全局"拥有全部"集合。它看起来像这样:
public class NPISNote
{
// properties
}
public class NPISItem
{
public ObservableCollection<NPISNoteBO> Notes { get; set; }
}
public class ViewModel
{
public ObservableCollection<NPISItemBO> NPISItemsList { get; set; }
}
<telerik:RadGridView ItemsSource={Binding NPISItemsList, Source={StaticResource NPISViewModel}}>
// columns
<telerik:RadGridView.RowDetailsTemplate>
<DataTemplate>
<telerik:RadGridView ItemsSource={Binding Notes}>
// columns
</telerik:RadGridView>
</DataTemplate>
</telerik:RadGridView.RowDetailsTemplate>
</telerik:RadGridView>
在添加新行以NPISItemsList
使用与新数据相关的信息填充其子集合之前,绑定将完成其余的工作。