Problem with Silverlight Binding

Ok, so I tried to implement the coverage found on codeplex http://silverlightcoverflow.codeplex.com/

I wanted to use my own class to bind data:

class CoverItem { BitmapImage _image; string _title; string _link; string _content; public BitmapImage Image { get { return _image; } set { _image = value; } } public string Title { get { return _title; } set { _title = value; } } public string Link { get { return _link; } set { _link = value; } } public string Content { get { return _content; } set { _content = value; } } } 

This is the XAML for the codeplex cover user control:

 <custom:CoverFlowControl.ItemTemplate> <DataTemplate> <StackPanel> <Image Source="{Binding Image}" Width="300" /> <TextBlock Text="{Binding Title}" Width="300" /> <TextBlock Text="Testing" Width="300" /> </StackPanel> </DataTemplate> </custom:CoverFlowControl.ItemTemplate> 

The problem I am facing is that I get the word "Test" for every related item, but I do not get the image or title that belong to my objects attached to the ItemSource property of the control.

 Covers.ItemsSource = _items; 

My question is: where am I mistaken? This should be a simple snap, so I think I'm missing something.

Thanks in advance for your help.

EDIT:

If I change the code to this:

 List<BitmapImage> images = new List<BitmapImage>() { _items[0].Image, _items[1].Image, _items[2].Image, _items[3].Image }; Covers.ItemsSource = images;// _items; 

And then bind it like:

 <Image Source="{Binding}" Width="300" /> 

Now I get my images. So I know this is a binding problem somewhere.

Also tried

 <Image Source="{Binding Path=Image}" Width="300" /> 
+1
source share
2 answers

Make the CoverItem class public. One of the drawbacks of Silverlight is that allowing reflection on internal types through assemblies is not allowed. When binding to CLR properties, reflection is used to obtain the value. The assembly that is trying to get the value is System.Windows, and it will not have permission to reflect the internal type in your assembly.

I wrote about this in the context of anonymous types (which are internal types): http://surrealization.com/blog/silverlight-anonymous-type-binding-gotcha/

Alternatively, you can provide the InternalsVisibleTo attribute on your assembly to allow System.Windows to "see" your internal type.
http://grahammurray.wordpress.com/2010/05/30/binding-to-anonymous-types-in-silverlight/

For a description of the horse, see this MSDN link:
http://msdn.microsoft.com/en-us/library/stfy7tfc(VS.95).aspx

In Silverlight, you cannot use reflection of access to private types and members. If the type of access level or member can prevent you from accessing it in statically compiled code, you cannot access it dynamically using reflection.

AND
http://connect.microsoft.com/VisualStudio/feedback/details/526229/in-silverlight-4-binding-to-an-internal-data-by-code-doesnt-work

Silverlight supports binding to public types only.

+4
source

I have not tested the CoverFlow project yet, but from the way you define the bindings in your XAML, the Image property in your model is usually considered Uri instead of the actual image control:

  public Uri Image { get; set; } // should also be renamed to ImageUri 

This is a cleaner anyway. If you really really have to use the actual Image in your model, you will need to define a ContentPresenter in your XAML.

As for why the Title will not be displayed, I do not know.

0
source

Source: https://habr.com/ru/post/1384361/


All Articles