绑定列表的可观察集合

本文关键字:观察 集合 列表 绑定 | 更新日期: 2023-09-27 18:36:37

在我的Windows Phone应用程序中,我有一个绑定了ContentItems的列表框:

private ObservableCollection<ContentItemViewModel> _contentItems;
public ObservableCollection<ContentItemViewModel> ContentItems
{
    get { return _contentItems; }
}
 <ListBox x:Name="ContentListBox"   Margin="0,117,12,0"  VirtualizingStackPanel.VirtualizationMode="Standard" Logic1:TiltEffect.IsTiltEnabled="True" ItemsSource="{Binding ContentItems}"   Tap="ContentListBox_Tap" MinHeight="656"  >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="vertical" >
                <Grid Height="{Binding ItemHeight}" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Image   Grid.Column="0"  x:Name="itemIco1"  Width="Auto" Height="Auto" HorizontalAlignment="Left" Source="{Binding IconURL}" Stretch="Fill" CacheMode="BitmapCache" VerticalAlignment="Top" Margin="5,5,5,5" Visibility="Visible"/>
                    <ListBox IsHitTestVisible="False" Grid.Column="1"  VerticalAlignment="Center"  >
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <TextBlock x:Name="ContentCategoryTitle" Height="70" Text="{Binding ContentTitle}" Width="460"  Margin="5,34,0,0"  TextTrimming="WordEllipsis" TextWrapping="NoWrap"    FontSize="28" FontFamily="Segoe WP Semibold"   Foreground="#FFF7F7F7"  VerticalAlignment="Bottom" />
                    </ListBox>
                </Grid>
                <Rectangle Fill="#FF585858" Height="1" Margin="0,0,0,0"  Width="460" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

是否可以绑定不是ObservableCollection<ContentItemViewModel>,而是 - ObservableCollection<List<ContentItemViewModel>>

绑定列表的可观察集合

是的,如果你有一个集合,这是可能的。或者为什么不是这个?

ObservableCollection<ObservableCollection<ContentItemViewModel>>

如果希望 UI 收到子集合更改的通知。

更新

例如:

查看模型

public ObservableCollection<ObservableCollection<ContentItemViewModel>> ContentItems
{
   get { return _contentItems; }
   set { _contentItems = value; // Notify of property change here, this allows you to change the ContentItems reference after view model construction }
}
public MyViewModel()
{
    // Populate content items
    this.ContentItems = new ObservableCollection 
          {
              new ObservableCollection { new ContentItemViewModel() },
              new ObservableCollection { new ContentItemViewModel(), new ContentItemViewModel() }
          };
}

视图

<ListBox ItemsSource="{Binding ContentItems}" ...>
   <ListBox.ItemTemplate>
      <DataTemplate>
         <Grid>
            <ListBox ItemsSource="{Binding}">
               <ListBox.ItemTemplate>
                 <DataTemplate>
                    <Grid>
                       <TextBlock Text="{Binding MyContentItemViewModelProperty}" />
                    </Grid>
                 </DataTemplate>
               </ListBox.ItemTemplate>
            </ListBox>
         </Grid>
      </DataTemplate>
    <ListBox.ItemTemplate>
 </ListBox>