Image HeightRequest via MVVM

ShaunBauerShaunBauer USMember ✭✭

Hi all,

Struggling to set the HeightRequest of an image via binding using C#. I thought it would be as simple as this:

var image = new Image { };
image.SetBinding(HeightRequest, "ImageHeight");

However, that results in

Cannot convert from double to Xamarin.Forms.BindingBase

If I was using XAML, it looks to be pretty much however XAML isn't an option as the elements are built dynamically from a DB.

For reference, my ViewModel is:

    public partial class QuestionsViewModel : INotifyPropertyChanged
    {

        private int inputHeight = 50;

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public QuestionsViewModel(bool populateList = true)
        {

        }

        public int InputHeight
        {
            get { return inputHeight; }
            set { inputHeight = value; }
        }
    }

Binding is working elsewhere via the SetBinding() function - this is the first time I have tried to set a HeightRequest via binding.

Thanks!

Best Answer

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭

    Replace your code with this

    Image image = new Image();
    image.SetBinding(Image.HeightRequestProperty, new Binding("ImageHeight"));
    
  • ShaunBauerShaunBauer USMember ✭✭

    Thanks @AlessandroCaliaro . I was trying to bind to the wrong property.

    InputHeight vs ImageHeight was the result of a rushed refactor to cull out all the noisy code for the post... good pickup ;)

Sign In or Register to comment.