DynamicResource 成员不更新
我为列表框设置了这个数据模板:
<DataTemplate x:Key="SlideListItem">
<StackPanel>
<Border Margin="1" BorderBrush="#505050" BorderThickness="1">
<Border.Effect>
<DropShadowEffect ShadowDepth="1" BlurRadius="3" Opacity=".5" />
</Border.Effect>
<Image Source="{Binding Thumbnail}" Stretch="Fill" VerticalAlignment="Center"/>
</Border>
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
因此它显示图像及其下面的一些文本。 这是列表框:
<ListBox x:Name="PageList" Grid.Column="0" ItemsSource="{DynamicResource SlideList}"
ItemTemplate="{StaticResource SlideListItem}" MouseDown="PageList_MouseDown" SelectionChanged="PageList_SelectionChanged" />
SlideList 是一个动态资源,更准确地说是我自己的类的 ObservableCollection。该类非常简单,只有几个属性(一些字符串、图像和列表)。
问题是缩略图需要每隔几秒更新一次,但是当我尝试从 C# 更新它时,它不会更新。我发现让它显示更新的唯一方法是删除 DynamicResource,然后再次添加它。
I have this data template set up for a listbox:
<DataTemplate x:Key="SlideListItem">
<StackPanel>
<Border Margin="1" BorderBrush="#505050" BorderThickness="1">
<Border.Effect>
<DropShadowEffect ShadowDepth="1" BlurRadius="3" Opacity=".5" />
</Border.Effect>
<Image Source="{Binding Thumbnail}" Stretch="Fill" VerticalAlignment="Center"/>
</Border>
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
So it is displaying an image, and some text under it.
This is the list box:
<ListBox x:Name="PageList" Grid.Column="0" ItemsSource="{DynamicResource SlideList}"
ItemTemplate="{StaticResource SlideListItem}" MouseDown="PageList_MouseDown" SelectionChanged="PageList_SelectionChanged" />
SlideList is a dynamic resource, more exactly an ObservableCollection of my own class. The class is pretty simple, only has few properties (some strings, an image, and a list)
The problem is that the Thumbnail needs to update every few seconds, but when I try to update it from c#, it doesn't update. The only way I found to make it show the updates is to delete the DynamicResource, and then add it again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是在更改 Thumbnail 属性还是在替换 ObservableCollection 中的项目?
在第一种情况下,您的简单类必须实现 INotifyPropertyChanged。否则,绑定目标只会在更改集合(例如替换项目、添加新项目等)时更新,而不是在更改集合中的项目时更新。
Are you changing the Thumbnail property or are you replacing the item in your ObservableCollection?
In the first case your simple class has to implement INotifyPropertyChanged. Otherwise the binding target will only be updated when changing the collection e.g. replacing an item, adding new items etc., not when changing an item in the collection.