Reference custom resource defined in another xaml file

。_饼干妹妹 提交于 2019-11-28 06:28:40

In WPF, the resource references works as a tree. Each control have resource, and children control can access parent's resources. The global application resource dictionary is in the App.xaml file. In this file you can include several resource dictionaries as a Merged Dictionary. See this code sample:

<?xml version="1.0" encoding="utf-8"?>
<Application ...>
    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="View\SomeFileDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

The SomeFileDictionary.xaml is located in the View folder of my project structure. And has looks like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:ViewModel="clr-namespace:Cepha.ViewModel"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                ... >

<DataTemplate DataType="{x:Type ViewModel:SomeType}">
    <TextBox .../>
</DataTemplate>...

And each dictionary key or data template defined in this file (or App.xaml), can be referenced in any place of your project. Hope this helps...

You should define this in the app.xaml file. These resources are shared throughout the entire project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!