开发者

Using generic collections in Silverlight user controls

开发者 https://www.devze.com 2023-01-07 07:18 出处:网络
There is an interface:开发者_如何学运维 public interface IFoo { } A Silverlight user control has a collection of IFoo instances:

There is an interface:开发者_如何学运维

public interface IFoo {
}

A Silverlight user control has a collection of IFoo instances:

public ObservableCollection<IFoo> Items { get; set; }

There is an abstract class that implements the interface:

abstract public class Foo : IFoo {}

And a class that further derives from that:

public class DerivedFoo : Foo {}

With all of that said, I'm trying to add instances of DerivedFoo into the control's collection via XAML, but I receive an error that DerivedFoo is not of type IFoo and cannot be used in the generic collection.

I did find a post in a forum that said this was a bug in Silverlight 3 but would be fixed (I am using Silverlight 4). Is this still a bug or am I going about this incorrectly?

Update: My code is at home and I'm at work so I can't post the actual XAML, but from memory it was along the lines of:

<my:Thing>
    <my:Thing.Items>
        <my:DerivedFoo ... />
    </my:Thing.Items>
</my:Thing>


The answer is...

The CollectionChanged event handler for the generic collection made an improper cast during the Add action.

0

精彩评论

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