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

发布于 2025-01-04 23:13:24 字数 2381 浏览 0 评论 0原文

我有两个类:

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>

一切都很好地触发(即我看到两个数据分页器,这意味着它看到两个条目),除了 < 的 get 方法RecordingsCollection 中的 code>Recordings 属性 有什么想法吗?

编辑 找到了问题...我应该绑定到 Recordings,而不是绑定到 RecordingsCollection...现在一切正常...希望这对将来的人有帮助:)

I have a two classes:

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);
        }
    }
}

At the momment I have only dummy data

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 });

I bind this all to an items control like this

<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>

Everything is fired fine (i.e. i see two datapagers which means that it sees two entries) except for the get method of the Recordings property in RecordingsCollection
Any ideas?

Edit
Found the issuse... Instead of binding to RecordingsCollection I should have bound to Recordings... Now everything works fine... Hope this helps someone in future :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2025-01-11 23:13:24

这就是答案

<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>   

This is the answers

<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>   

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文