c# - 项目的可观察集合不触发转换器
问题描述
我有一个这样的 xaml(我删除了无用的行)
<Window x:Class="LaRenouvellerieUpdater.MainWindow"
xmlns:converters="clr-namespace:LaRenouvellerieUpdater.Converters">
<Window.Resources>
<ResourceDictionary>
<viewmodel:DataViewModel x:Key="DataViewModel"/>
<model:PhotosModel x:Key="PhotosModel"/>
<converters:UriToCachedImageConverter x:Key="UriToCachedImageConverter"/>
</ResourceDictionary>
</Window.Resources>
<Grid>
<TabControl>
<TabItem Header="Posts instagrams">
<DataGrid ItemsSource="{Binding InstagramsPhotos, Mode=TwoWay}"
DataContext="{Binding Source={StaticResource DataViewModel}}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Url" Width="*" MaxWidth="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=Url, Converter={StaticResource UriToCachedImageConverter}, Mode=TwoWay}" DecodePixelWidth="150" />
</Image.Source>
</Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
还有一个像这样的可观察集合:
private ObservableCollection<PhotosModel> _instagramsPhotos = new ObservableCollection<PhotosModel>();
public ObservableCollection<PhotosModel> InstagramsPhotos
{
get => _instagramsPhotos;
set
{
_instagramsPhotos = value;
RaisePropertyChanged();
}
}
像这样的照片模型:
public PhotosModel(Action<object, PropertyChangedEventArgs> photosModel_PropertyChanged)
{
PropertyChanged = new PropertyChangedEventHandler(photosModel_PropertyChanged);
}
private void NotifyPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private string _url;
public string Url
{
get => _url;
set
{
_url = value;
NotifyPropertyChanged("Url");
}
}
当我在我的 observablecollection 中推送元素时,我的 Converter UriToCachedImage 永远不会触发。我不知道为什么。如果属性被修改但不起作用,我试图将我的可观察集合传递给所有 PhotosModel 以触发它。
谢谢
解决方案
我通过在我的类中创建一个执行转换器工作的属性来解决这个问题。这不完全是我想要的,但它有效。
推荐阅读
- reactjs - 功能在本机反应中执行问题
- c - Libwebsocket 读取大消息
- awk - 无法在邮件正文中以 HTML 表格格式打印
- python - 如何正确设置 AddDimension 中的 SetGlobalSpanCostCoefficient 和容量参数?
- python - Python - 使用烧瓶获取带有&符号的args字符串
- javascript - 如何使用 css 调整 div 的行和列的大小
- wordpress - 主页 - 产品在移动设备中比屏幕更宽 - 如何修复?
- python - 我已删除 python 但仍无法从安装程序重新安装
- ios - 如何解决 UIImages 未在 Xcode 的模拟器或 iPhone 中显示的问题
- spring-integration - Spring Batch Integration:SftpPersistentAcceptOnceFileListFilter 持久化来自 FTP 服务器的文件但不被 Joblauncher 处理