简体   繁体   English

如何从UserControl的外部Window继承资源?

[英]How to inherit resources from outer Window in an UserControl?

I start my WPF app per Windows Application new Application().Run(new MainWindow()) - so I don't have an app.xaml 我按照Windows应用程序new Application().Run(new MainWindow())启动WPF应用程序-所以我没有app.xaml

I have a main window, which contains any used UserControls per ContentControl 我有一个主窗口,其中每个ContentControl包含所有使用过的UserControl

<Window ..>
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..." />
                <!-- ... -->
            <converter:BooleanToIntConverter x:Key="BooleanToIntConverter" />
        </ResourceDictionary>
    </Window.Resources>

    <ContentControl Grid.Column="0" Content="{Binding MyUserControl}" />
</Window>

How can I inherit resources from my MainWindow to UserControls which are within iny MainWindow? 如何将资源从MainWindow继承到iny MainWindow中的UserControls?

You can set the resources of the Application by accessing the Resources ResourceDictionary : 您可以设置的资源 Application通过访问Resources ResourceDictionary

Application app = new Application();
app.Resources["MySharedResource"] = ...;
app.Resources["MyOtherSharedResource"] = ...;
app.Run(new MainWindow());

This is exactly what is done with the App.xaml file. 这正是使用App.xaml文件完成的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM