2011-10-06 118 views
0
<Window.Resource> 
    <ResourceDictionary> 
     <local:SomeResourceWithObsCollection x:Key="MyItemWithCollection"> 
      <local:SomeClass.Instance /> <!-- THIS DOES NOT WORK --> 
     </local:SomeResourceWithObsCollection> 
    </ResourceDictionary> 
</Window.Resources> 

我不知道如何让这条线路工作...我试过做<x:Static SomeClass.Instance />,但这也是不允许的。带静态项目的XAML资源?

[ContentProperty("TheItems")] 
public class SomeResourceWithObsCollection 
{ 
    public class SomeResourceWithObsCollection() 
    { 
     TheItems = new ObservableCollection<IMyInterface>(); 
    } 

    public ObservableCollection<IMyInterface> TheItems { get; set; } 
} 

public class SomeClass : IMyInterface 
{ 
    private static SomeClass _instance = new SomeClass(); 

    private SomeClass() { } 

    public SomeClass Instance { get { return _instance; } } 
} 
+0

你在* *试图得到它呢?你可以发布“SomeResourceWithObsCollection”类型的细节吗? –

+0

我发布了实际上是相关的东西。基本上我有单身实例。如果我删除这些项目是单身的事实,一切正常。但是,我想基本上能够在XAML中使用单例实例。 – michael

回答

1

我可以建议最接近的是CompositeCollection的组合,并使用ListBoxItems(或其他等值)来包装你的静态内容(因为我相信你只能使用{x:Static}标记扩展拉静态内容到XAML)

这可以在XAML中使用如下:

<ListBox> 
    <ListBox.ItemsSource> 
     <CompositeCollection> 
      <ListBoxItem Content="{x:Static local:Example.One}" /> 
      <ListBoxItem Content="{x:Static local:Example.Two}" /> 
     </CompositeCollection> 
    </ListBox.ItemsSource> 
</ListBox> 
+0

我不想得到一个ListBoxItem。我想要一个我的对象的实例作为资源加载到我的obs集合中。 – michael