0

I am binding an Image control to a property of an array element like so:

<Image Name="Image1" Source="{Binding ImageWpf}" ... />

and in the code behind:

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Image1.DataContext = _monster.Genes[0];
            // ...
        }

The array is a property of an object which itself is a member of the window:

public partial class WindowGenes : Window
{
   public Monster _monster;
   // ...
}

this is the array-property from the Monster class:

        public Gene[] Genes
        {
            get
            {
                Gene[] g = new Gene[9];
                for (int i = 0; i < 9; i++)
                {
                    var read = BitConverter.ToUInt32(_data, _startOffset + OFFSET_GENES + i * 4);
                    g[i] = Gene.CreateFromValue( read );
                }
                return g;
            }
            set
            {
                for (int i = 0; i < 9; i++)
                {
                    var bt = BitConverter.GetBytes(value[i].Id);
                    Array.Copy(bt, 0, _data, _startOffset + OFFSET_GENES + i * 4, 4);
                }
            }
        }

to complicate things further, the array is generated in the properties getter, because this is created from a byte-array which represents a save file. Meaning, each read/get creates another array.

The binding itself seems to work. When the window is loaded it shows the proper Image. But the images do not change. I know there is the IPropertyChanged-interface but where should i implement that and how? I mean it wouldn't make sense to implement that for the ImageWpf property because the arrays items themselves (and also the array) are changed and not just the ImageWpf property.

Clemens
  • 123,504
  • 12
  • 155
  • 268
Roku
  • 31
  • 2
  • The class Gene should implement INotiyPropertyChanged and fire the PropertyChanged event in the setter of its ImageWpf property. – Clemens Oct 03 '22 at 16:32
  • ImageWpf is never changed directly it is a read-only property, it creates a "BitmapImage" depending on other variables values. I have placed the firing of PropertyChanged inside the constructor of the Gene object, because this is the only place where the variables that flow into the bitmap generation are written to. But it does not help, images still stay the same. Still thanks for trying to help – Roku Oct 03 '22 at 18:09
  • In case what you are saying means that Gene instances are immutable, and you replace them in the Genes array, simply replace that array by an ObservableCollection. – Clemens Oct 03 '22 at 18:22
  • If you replace the entire array, make Monster implement INotifyPropertyChanged and fire the PropertyChanged event in the Genes setter. Make sure to set the DataContext to the Monster instance then. – Clemens Oct 03 '22 at 18:23
  • thank you very much, that solved my problem. Changed the binding to "{Binding Genes[0].ImageWpf}" and the DataContext to "_monster" then implemented the interface in Monster-class and told WPF that "Genes" had changed. – Roku Oct 04 '22 at 12:27

0 Answers0