项控制与两个可观察集合的绑定

本文关键字:观察 集合 绑定 两个 控制 | 更新日期: 2023-09-27 18:24:36

我有两个类:

public class ScheduleViewModel : NotificationObject
{
    private ObservableCollection<RecordingsCollection> _collection
    public ObservableCollection<RecordingsCollection> Collection
    {
        get { return _collection; }
        set
        {
            _collection= value;
            RaisePropertyChanged(() => Collection);
        }
    }
}
public class RecordingsCollection : NotificationObject
{
    private ObservableCollection<Recording> _recordings;
    public ObservableCollection<Recording> Recordings
    {
        get { return _recordings; }
        set
        {
            _recordings = value;
            RaisePropertyChanged(() => Recordings);
        }
    }
}

目前我只有伪数据

var a = new ObservableCollection<Recording>();
a.Add(new Recording()
{
   Name = "Bla bla",
   Schedule = new Schedule()
   {
       Name = "bla"
   }
});
Collection.Add(new RecordingsCollection() { Recordings = a });
var b = new ObservableCollection<Recording>();
b.Add(new Recording()
{
   Name = "Bla bla",
   Schedule = new Schedule()
   {
       Name = "bla"
   }
});
Collection.Add(new RecordingsCollection() { Recordings = b });

我把这些都绑定到一个类似的项目控件上

<ItemsControl Grid.Row="1" ItemsSource="{Binding Collection}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
       <Grid>
         <telerikGridView:RadGridView
             ItemsSource="{Binding ElementName=RecordingDataPager, Path=PagedSource}">
             <telerikGridView:RadGridView.Columns>
                 <telerikGridView:GridViewDataColumn Header="Schedule" DataMemberBinding="{Binding Path=Recordings.Schedule.Name}"/>
                 </telerikGridView:RadGridView.Columns>
             </telerikGridView:RadGridView>
         <telerik:RadDataPager x:Name="RecordingDataPager"
                  Source="{Binding RecordingsCollection, Mode=TwoWay}"/>
        </Grid>
     </DataTemplate>
   </ItemsControl.ItemTemplate>
 </ItemsControl>

除了RecordingsCollection中的Recordings属性的get方法外,其他一切都很好(即,我看到两个数据寻呼机,这意味着它看到两个条目)有什么想法吗?

编辑发现问题。。。与其绑定到RecordingsCollection,我应该绑定到Recordings。。。现在一切都很好。。。希望这对将来的人有所帮助:)

项控制与两个可观察集合的绑定

这是的答案

<ItemsControl Grid.Row="1" ItemsSource="{Binding Collection}">   
  <ItemsControl.ItemTemplate>   
   <DataTemplate>   
    <Grid>   
     <telerikGridView:RadGridView   
         ItemsSource="{Binding ElementName=RecordingDataPager, Path=PagedSource}">   
         <telerikGridView:RadGridView.Columns>   
             <telerikGridView:GridViewDataColumn Header="Schedule" DataMemberBinding="{Binding Path=Schedule.Name}"/>   
             </telerikGridView:RadGridView.Columns>   
         </telerikGridView:RadGridView>   
     <telerik:RadDataPager x:Name="RecordingDataPager"   
              Source="{Binding Recordings, Mode=TwoWay}"/>   
    </Grid>   
 </DataTemplate>