Silverlight绑定问题 [英] Silverlight Binding Issue

查看:110
本文介绍了Silverlight绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我试图实现一个在codeplex上找到的封面。 http://silverlightcoverflow.codeplex.com/

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; }
        }
    }

这是封面用户控制的XAML codeplex:

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>

我遇到的问题是我对每个绑定的元素都进行了测试,但是我没有得到图像或标题,这是从我附加到控件的ItemSource属性的对象。

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.

提前感谢帮助。

编辑:

如果我将代码更改为:

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

Covers.ItemsSource = images;// _items;

然后有这样的绑定:

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

我现在看到我的图像显示。所以我知道这是绑定某处的一个问题。

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

还尝试过

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


推荐答案

使 CoverItem class public。 Silverlight的缺点之一是不允许跨组件的内部类型的反射许可。当绑定到CLR属性时,使用反射来获取值。试图获取该值的程序集是System.Windows,它将不具有反映程序集中内部类型的权限。

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.

我已经写了这个在匿名类型(内部类型)的上下文中:
http: //surrealization.com/blog/silverlight-anonymous-type-binding-gotcha/

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

另外,您可以在程序集中提供InternalsVisibleTo属性,以允许系统.Windows以查看您的内部类型。

http://grahammurray.wordpress.com/2010/05/30/binding-to-anonymous-types-in-silverlight/

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/

对于马的嘴描述,请参阅此MSDN链接:

http://msdn.microsoft.com/en-us/libra ry / stfy7tfc(VS.95).aspx

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


在Silverlight中,您不能使用
反射访问私有类型和
成员。如果类型
或成员的访问级别将阻止您从
以静态编译的
代码访问它,则无法使用反射动态访问它

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.



http://connect.microsoft.com/VisualStudio/feedback/details/526229/in-silverlight -4绑定到一个内部数据的代码不工作


Silverlight支持绑定到public
类型。

Silverlight supports binding to public types only.

这篇关于Silverlight绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆