使用静态资源填充 XAML 中 ObservableCollection 类型的属性
我创建了一个包含 ObservableCollection 类型的属性的类。我正在尝试在 XAML 中创建该类的实例并用成员填充此属性。我不断收到类 T 无法转换为 ObservableCollection 的异常,但仅当我尝试使用声明为静态资源的元素填充列表时才会发生此异常。
有人知道为什么吗?
代码如下:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mseg="clr-namespace:SegApp.Model.DataEntities.Segments;assembly=SegApp.Model.Silverlight">
<mseg:Dot xKey="d1"/>
<mseg:Dot xKey="d2"/>
<mseg:Dot xKey="d3"/>
<mseg:Dot xKey="d4"/>
<mseg:Segment xKey="seg1">
<mseg:Segment.Dots>
<StaticResource ResourceKey="d1"/>
<StaticResource ResourceKey="d2"/>
<StaticResource ResourceKey="d3"/>
<StaticResource ResourceKey="d4"/>
</mseg:Segment.Dots>
</mseg:Segment>
</ResourceDictionary>
类定义为:
public class Segment : Part
{
public ObservableCollection<Dot> Dots { get; set; }
public Segment()
{
Dots = new ObservableCollection<Dot>();
}
}
异常情况如下: ”
bla.bla.bla.Dot 类型的对象不能 被转换为类型 System.Collections.ObjectModel.ObservableCollection'1[bla.bla.bla.Dot]
"
有什么想法吗?
I have created a class that contains a property of type ObservableCollection. I am trying to create an instance of the class in XAML and fill this property with members. I keep getting an exception that class T can not be converted to ObservableCollection, but this exception only occurs when I am trying to populate the list with elements that were declared as static resources.
Anybody has an idea why?
The code is as follows:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mseg="clr-namespace:SegApp.Model.DataEntities.Segments;assembly=SegApp.Model.Silverlight">
<mseg:Dot xKey="d1"/>
<mseg:Dot xKey="d2"/>
<mseg:Dot xKey="d3"/>
<mseg:Dot xKey="d4"/>
<mseg:Segment xKey="seg1">
<mseg:Segment.Dots>
<StaticResource ResourceKey="d1"/>
<StaticResource ResourceKey="d2"/>
<StaticResource ResourceKey="d3"/>
<StaticResource ResourceKey="d4"/>
</mseg:Segment.Dots>
</mseg:Segment>
</ResourceDictionary>
The Class definition is:
public class Segment : Part
{
public ObservableCollection<Dot> Dots { get; set; }
public Segment()
{
Dots = new ObservableCollection<Dot>();
}
}
And the exception says:
"
Object of type bla.bla.bla.Dot can not
be converted to type
System.Collections.ObjectModel.ObservableCollection'1[bla.bla.bla.Dot]
"
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您的代码一样,集合的每个元素都必须是一个点,而不是资源......
xaml 代码中列表的每个条目必须类似于
或者也许尝试
某个值
或者
{staticResource xxx }
但还是有问题。第一个语法没问题,如果 Dot 有简单的内容,第二个语法可以工作,但第三个语法无法运行:标记意味着“创建 Dot 的实例”。 StaticResource 的意思是“创建......的实例并给它一个密钥”。
因此,最后一个语法肯定不起作用,因为您可以用来自资源的实例替换由标签创建的实例...
但是请尝试一下。代码中的主要问题是您试图感受带有资源的点的集合,这是行不通的,编译器也不好。尝试使用标签来创建条目。然后玩一下,看看是否可以在这些标签中的某个位置引用资源。
As is your code, each element of the collection must be a Dot, not a resource...
Each entry of the list in your xaml code must be something like
or perhaps try
somevalue
or
{staticResource xxx }
But there is still a problem. The 1st syntax is ok, the second can work if there is a simple content for Dot, but the 3rd can't run : tag means "create an instance of Dot". And a StaticResource means "create an instance of.. and give it a key".
So last syntax will certainly not work cause you can replace the instance created by the tag with the instance coming from the resource...
But give it a try. The main problem in your code is than you're trying to feel a collection of Dot with Resource, that can't work and the compiler is not ok.. try using tag to create entry. And then play a bit to see if you can refer the resources somewhere in these tags..
为了使用集合 XAML 语法,请更改您的属性并删除它的 setter:
In order to use collections XAML syntax change your property and remove it's setter: