zoukankan      html  css  js  c++  java
  • 【WPF】给UserControl引入多个资源

    问题:为了方便资源的复用,我们通常会把资源单独抽取为一个资源文件,供其他文件引用。而用户自定义控件UserControl中经常需要引入多个资源文件。而在XAML中由于标签UserControl.Resources内仅可以包含一个Content子元素。

    所以为了给UserControl引入多个资源,XAML中应该这么写:

    <UserControl
        x:Class="HomeDecorationPSD.Presentation.Views.UiWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:HomeDecorationPSD.Presentation.Views"
        mc:Ignorable="d"
        xmlns:system="clr-namespace:System;assembly=mscorlib"       
        >
        <!-- 引入多份资源 -->
        <UserControl.Resources>
            <ResourceDictionary><!-- 重点:需要使用这个标签来包含多个内容 -->
                <!-- 资源1:来自系统类 -->
                <system:Double x:Key="TabItemWidth">80</system:Double>
                <!-- 资源2:字典类的资源 -->
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/Presentation/Resources/ColorResources.xaml"></ResourceDictionary><!-- 项目工程的相对路径 -->
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </UserControl.Resources>
    
        <!-- 接下来使用资源 -->
    
    </UserControl>

    其他类似的用法

    参考:https://stackoverflow.com/questions/1333786/how-to-combine-imported-and-local-resources-in-wpf-user-control

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ViewResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <!-- This works: -->
            <ControlTemplate x:Key="validationTemplate">
                ...
            </ControlTemplate>
            <style x:key="textBoxWithError" TargetType="{x:Type TextBox}">
                ...
            </style>
            ...
        </ResourceDictionary>
    </UserControl.Resources>
     
  • 相关阅读:
    UVA1599 理想路径 Ideal Path(最短路径)
    换根DP
    小w的魔术扑克(树状数组+并查集)
    NOIP 2016蚯蚓(优先队列)
    ZR 动物园
    T105017 seq(DP)
    noip2017酱油记
    noip2017酱油记前篇
    P1985 翻转棋
    luogu P2512 [HAOI2008]糖果传递
  • 原文地址:https://www.cnblogs.com/guxin/p/wpf-usercontrol-how-to-include-multi-resource.html
Copyright © 2011-2022 走看看