开发者

Silverlight Binding Issue

Ok, So I have tried to implement a coverflow found on codeplex http://silverlightcoverflow.codeplex.com/

I wanted to use my own class for data binding:

    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 Cover User Control from codeplex:

<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 having is that I get word "Testing" for each element that was bound, but I am not getting image or the title, which are from my objects that attached to the ItemSource property of the control.

Covers.ItemsSource = _items;

My question is, where am I going wrong? This should be a simple binding, so think I am missing something.

Thanks in advance for the help.

EDIT:

If I change the code to this:

List<BitmapImage> images = new List<BitmapImage>() { _item开发者_如何学运维s[0].Image, _items[1].Image, _items[2].Image, _items[3].Image };

Covers.ItemsSource = images;// _items;

And then have the binding as this:

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

I now get my images displaying. So I know it is a problem with the binding somewhere.

Have also tried

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


Make the CoverItem class public. One of the downsides of Silverlight is reflection permission on internal Types across assemblies is not allowed. When binding to CLR properties, reflection is used to get the value. The assembly that's trying to get the value is System.Windows, and it won't have permission to reflect an internal Type in your assembly.

I've written about this in context of anonymous Types (which are internal Types): http://surrealization.com/blog/silverlight-anonymous-type-binding-gotcha/

Alternately you can provide an 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 from-the-horse's-mouth description, see this MSDN link:
http://msdn.microsoft.com/en-us/library/stfy7tfc(VS.95).aspx

In Silverlight, you cannot use reflection to access private types and members. If the access level of a type or member would prevent you from accessing it in statically compiled code, you cannot access it dynamically by 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.


I haven't checked that CoverFlow project out yet, but from the way you define the bindings in your XAML, the Image property in your model is usually expected to be an Uri instead of an actual Image control:

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

That's cleaner anyway. If you really, really must use an actual Image in your model, you'll have to define a ContentPresenter in your XAML.

As for why the Title would not display, I don't know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜