【问题标题】:Observable collection of item do not trigger Converter项目的可观察集合不触发转换器
【发布时间】:2021-07-05 09:20:27
【问题描述】:

我有一个这样的 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 永远不会触发。我不知道为什么。 我尝试将我的 observable 集合传递给所有 PhotosModel 以在属性被修改但不起作用时触发它。

谢谢

【问题讨论】:

  • 将事件处理程序传递给模型并用= 分配它看起来很奇怪。没有正确的minimal reproducible example 很难说更多。某处缺少通知,因此转换器未运行。
  • 您是将InstagramsPhotos 分配给一个新对象还是使用Add 方法在其中添加项目?
  • 这样,PropertyEvent 在 Url 设置之前传递,因此通知会正确触发。当我创建一个新的 PhotosModel 时,它会触发 RaisePropertyChanged("InstagramPhotos")

标签: c# xaml binding observablecollection converters


【解决方案1】:

我通过在我的类中创建一个执行转换器工作的属性来解决此问题。这不完全是我想要的,但它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多