简体   繁体   English

WPF ItemsControl绑定在绑定到视图模型中的对象中的ObservableCollection时不会更新

[英]WPF ItemsControl binding not updating when bound to an ObservableCollection in an object in the View Model

Basically I have an object in my view model that contains an ObservableCollection of a custom object. 基本上,我的视图模型中有一个对象,其中包含一个自定义对象的ObservableCollection。 My XAML's DataContext is set to my ViewModel, my ViewModel contains a 'Scratchdisk' object, and the Scratchdisk object contains an ObservableCollection of Frame objects. 我的XAML的DataContext设置为ViewModel,ViewModel包含一个“ Scratchdisk”对象,而Scratchdisk对象包含一个Frame对象的ObservableCollection。 Both the Scratchdisk and the Collection are set up as DependencyProperties. 暂存盘和集合都设置为DependencyProperties。

In short: XAML --DataContext--> EditorViewModel --DependencyProperty--> Scratchdisk --DependencyProperty--> ObservableCollection<Frame> 简而言之: XAML --DataContext--> EditorViewModel --DependencyProperty--> Scratchdisk --DependencyProperty--> ObservableCollection<Frame>

The Frame object has 3 standard properties: Index, Image, and ImageUrl. Frame对象具有3个标准属性:Index,Image和ImageUrl。

I'm trying to bind to the ObservableCollection in my XAML using this code: 我正在尝试使用以下代码绑定到XAML中的ObservableCollection:

<ItemsControl DataContext="{Binding Source=ThumbnailScratchdisk}" ItemsSource="{Binding Frames, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource ThumbnailTemplate}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Horizontal"></VirtualizingStackPanel>
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
</ItemsControl>

Where ThumbnailTemplate is defined in Window Resources as: 在“窗口资源”中将ThumbnailTemplate定义为:

<DataTemplate x:Key="ThumbnailTemplate">
    <Image Width="128" Height="96" Source="{Binding ImageUrl}"/>
</DataTemplate>

Theoretically, what should happen is, the Scratchdisk should receive filenames, create Frame objects, add them to the Collection, and then the binding should display them. 从理论上讲,应该发生的事情是,暂存盘应该接收文件名,创建Frame对象,将它们添加到Collection中,然后绑定应该显示它们。 The ObservableCollection is working and being populated, but the binding doesn't seen to be updating. ObservableCollection正在运行并且正在填充,但是绑定没有被更新。 All the updatable properties are set as DependencyProperties so the binding should update shouldn't it? 所有可更新的属性都设置为DependencyProperties,因此绑定应该更新吗?

Links to the files: 链接到文件:

XAML XAML

ViewModel 视图模型

Scratchdisk 暂存盘

Frame

The problem is in the binding of the DataContext of your ItemsControl . 问题出在ItemsControlDataContext的绑定中。 You're setting it to "{Binding Source=ThumbnailScratchdisk}" , but what you (presumably) want is to set it to just "{Binding ThumbnailScratchdisk}" . 您将其设置为"{Binding Source=ThumbnailScratchdisk}" ,但是(大概)您希望将其设置为"{Binding ThumbnailScratchdisk}"

The DataContext of the page is already an instance of EditorViewModel , and you want the DataContext for the ItemsControl to bind to the property ThumbnailScratchdisk of that viewmodel. 页面的DataContext已经是EditorViewModel的实例,并且您希望ItemsControlDataContext绑定到该视图模型的属性ThumbnailScratchdisk

尝试将XAML中的绑定路径更改为ThumbnailScratchdisk.Frames

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM