How to use UI ViewModels to change images in image widgets

I’m trying to make a game on unreal engine 5. According to the lessons (and logic) in the blueprint widget, you can create a public image variable and change it whenever you want (in extreme cases, you can bind properties, although it calls every frame). In version 5.1, the ability to create public variables in the bluepirnt widget disappeared, epic games developers gave us ViewModel mechanism, which is only available only through C ++ by now. Question here: how do I change the image in the image widget (or any other widget with a picture) in the unreal engine 5.2.1?

i used this C++ ViewModel class:
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

include “CoreMinimal.h”
include “MVVMViewModelBase.h”
include “VMM_icons.generated.h”

/**
*
/
UCLASS(BlueprintType)
class GALAXYOFMYSTICHEROES_API UVMM_icons : public UMVVMViewModelBase
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, FieldNotify, Setter, Getter)
UTexture2D
MyIcon;

private:
    void SetMyIcon(UTexture2D* newTexture)
    {
        UE_MVVM_SET_PROPERTY_VALUE(MyIcon, newTexture);
        UE_MVVM_BROADCAST_FIELD_VALUE_CHANGED(GetIcon);
    }


    UTexture2D* GetMyIcon() const
    {
        return MyIcon;
    }

public:
    UFUNCTION(BlueprintPure, FieldNotify)
    UTexture2D* GetIcon() const
    {
        return MyIcon;
    }

};

this is the error i am getting
Binding ‘AbilityIMage.Brush.ResourceObject ← VMM_test.MyIcon’: Could not create binding. Couldn’t create the destination field path ‘AbilityIMage.Brush.ResourceObject’. Property Brush has getter accessor. Accessor not supported on FStructProperty since it would create a temporary structure and we would not able to return a valid container from that structure.

how to fix this?

I ran into the same error. Couldn’t figure out how to solve it while keeping my property as a UTexture2D pointer.

One workaround that does seem to solve it is to instead have an FSlateBrush as your view model property and then bind to the “Brush” property directly instead of “Brush.Image”.

Then in code you just need to do something like:

MyIconBrush.SetResourceObject(newTexture);
UE_MVVM_BROADCAST_FIELD_VALUE_CHANGED(MyIconBrush);

Not ideal perhaps as the slate brush is too view specific for the vm but it seems to work at least.

Also, just as a heads up I don’t think you need UE_MVVM_SET_PROPERTY_VALUE and UE_MVVM_BROADCAST_FIELD_VALUE_CHANGED at the same time, the set macro will broadcast if the value changes and the broadcast macro is more for if you can’t use the set one - so having both will cause your property to broadcast twice.