views:

27

answers:

1

Let's say I have the following dummy class:

public class Foo
    {
        public Image MyImage
        {
            get;
            set;
        }
    }

and I have the following in some XAML

<Image Source="{Binding Foo.MyImage}"/>

If I understand this correctly, this doesn't work because Source is expecting a URI string value for where MyImage is stored but in this case there is no URI because MyImage is in memory.

How can I get the above to work?

EDIT:

This is how MyImage is being created:

private void CreateBarCode(string bcValue)
    {
        Code128 bc128 = new Code128();
        bc128.HumanReadable = Code128.TextWhere.Below;
        this.MyImage = new Bitmap(bc128.Generate(bcValue)); 
    }

Via a method which takes a string value and returns a barcode which is in Bitmap format.

A: 

The Source property on the Image control is of type ImageSource. Your MyImage property has one of those accessible at MyImage.Source. So your xaml would look like

<Image DataContext={Binding Path=_anInstanceOfClassFoo} Source="{Binding Path=MyImage.Source}"/>

typically you don't set the DataContext directly, rather you'd let it inherit from parent controls. I only did that because in your example, your binding references the Foo class directly and not an instance

qntmfred
So even though MyImage is created via a method it still has a source?
Nathan
Source is just a property of type ImageSource on the System.Windows.Controls.Image class. How are you currently generating your MyImage object?
qntmfred
qntmfred please see my edit above.
Nathan