zoukankan      html  css  js  c++  java
  • WPF 使用ComponentResourceKey,隐藏代码,generic创建项目共享Resource

    Generic 代码 : 

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ResourceLib">
        <ResourceDictionary.MergedDictionaries >
            <ResourceDictionary Source="/ResourceLib;component/Styles1.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
        
        <Style TargetType="{x:Type local:CustomControl1}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
    
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>

    Styles1.xaml代码:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                         xmlns:local="clr-namespace:ResourceLib">
        
        <Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:Styles1},ResourceId=bigFontStyle}" >
            <Setter Property="Control.FontFamily" Value="Times New Roman"></Setter>
            <Setter Property="Control.FontSize" Value="18"></Setter>
            <Setter Property="Control.FontWeight" Value="Bold"></Setter>
        </Style>
    
    </ResourceDictionary>

    Styles1.cs代码

       public partial class Styles1
        {
            public static ComponentResourceKey BigFontStyleKey 
            {
                get 
                {
                    return new ComponentResourceKey(typeof(Styles1), "bigFontStyle");
                }
            }
        }
    

    ResourceStyle.xaml 代码:

    <Window x:Class="WPFLearning.StyleTemplates.ResourceStyle"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:rl="clr-namespace:ResourceLib;assembly=ResourceLib"
            Title="ResourceStyle" Height="300" Width="300">
        <Grid>
            <Button Style="{DynamicResource {x:Static rl:Styles1.BigFontStyleKey}}">Hi, i use a component sytle</Button>
        </Grid>
    </Window>

    ResourceLib.csproj 实现隐藏代码 

    <Compile Include="Styles1.cs">
    <DependentUpon>Styles1.xaml</DependentUpon>
    </Compile>

    显示效果 

    项目结构:

  • 相关阅读:
    Java知识系统回顾整理01基础04操作符02关系操作符
    Java知识系统回顾整理01基础04操作符01算术操作符
    Java知识系统回顾整理01基础03变量09块
    Java知识系统回顾整理01基础03变量08表达式
    Java知识系统回顾整理01基础03变量07final关键字
    Java知识系统回顾整理01基础03变量06变量的作用域
    Java知识系统回顾整理01基础03变量05变量命名规则
    Java知识系统回顾整理01基础03变量04类型转换
    leetcode-----64. 最小路径和
    leetcode-----63. 不同路径 II
  • 原文地址:https://www.cnblogs.com/mjgb/p/2665474.html
Copyright © 2011-2022 走看看