带有静态项目的 XAML 资源?

发布于 2024-12-08 13:54:22 字数 944 浏览 4 评论 0原文

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

我不知道如何让该行工作...我尝试过 ,但这也是不允许的。

[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; } }
}
<Window.Resource>
    <ResourceDictionary>
        <local:SomeResourceWithObsCollection x:Key="MyItemWithCollection">
            <local:SomeClass.Instance /> <!-- THIS DOES NOT WORK -->
        </local:SomeResourceWithObsCollection>
    </ResourceDictionary>
</Window.Resources>

I don't know how to get that line to work... I've tried doing <x:Static SomeClass.Instance />, but that also isn't allowed.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世态炎凉 2024-12-15 13:54:22

截至目前,您无法在 XAML 中执行您要求执行的操作。也许 XAML 的未来版本将解决这个问题。您必须在后面的代码中执行此操作,这是一个示例:

将静态对象添加到资源字典

You can't do what you're asking to do in XAML as of right now. Perhaps future versions of XAML will account for this. You have to do it in the code behind, here is an example:

Adding a static object to a resource dictionary

永言不败 2024-12-15 13:54:22

我可以建议的最接近的是 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>

The closest I can suggest is a combination of the CompositeCollection and using ListBoxItems (or some other equivalent) to wrap your static content (as I believe you can only pull static content into XAML using the {x:Static} markup extension)

This can be used in XAML as below:

<ListBox>
    <ListBox.ItemsSource>
        <CompositeCollection>
            <ListBoxItem Content="{x:Static local:Example.One}" />
            <ListBoxItem Content="{x:Static local:Example.Two}" />
        </CompositeCollection>
    </ListBox.ItemsSource>
</ListBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文