• 大小: 0.18M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2020-12-26
  • 语言: C#
  • 标签: wpf  组件  wp  

资源简介

本示例以TextBox为例,动态生成各种样式模板,并应用到组件。
原理:程序内置字符串为TextBox基础样式模板,通过输入的项目,即时修改字符串,
将字符串作为ResourceDictionary项,添加进Resources并调用。
此方式可应用于其他各种组件,本示例只演示了TextBox,组件其他项目也可设置,例如图片、图像效果等等,
不用多个xmal文件,均可用此方式设置。学习Wpf是个心塞的过程。


 /// <summary>
        /// 动态设置样式
        /// </summary>
        ///<param name="StyleName">样式名称</param>
        /// <param name="StyleName">样式名称</param>
        /// <param name="BorderColor">初始边框色</param>
        /// <param name="MouseOverColor">鼠标离开边框色</param>
        /// <param name="FocuseColor">焦点边框色</param>
        /// <param name="TextPading">文字距离边框</param>
        /// <param name="Radius">圆角度数</param>
        /// <param name="GradientBegin">背景渐变起始色</param>
        /// <param name="GradientEnd">背景渐变结束色</param>
        /// <returns></returns>
        public void DynamicStyle (
            TextBox textBox,
            string StyleName,
            string BorderColor, string MouseOverColor, string FocuseColor, 
            string TextPading, int Radius,
            string GradientBegin, string GradientEnd)
        {
            Resources.Remove(StyleName);
            string _style = @"
            <ResourceDictionary xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
            //////......样式略
             </ResourceDictionary>
            ";
            StringReader strreader = new StringReader(_style);
            XmlTextReader xmlreader = new XmlTextReader(strreader);
            object obj = XamlReader.Load(xmlreader);
            ResourceDictionary _ControlStyle = (ResourceDictionary)obj;
            Resources.MergedDictionaries.Add(_ControlStyle);
            textBox.Style = Resources[StyleName] as Style;
        }


资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace DynamicSetDEmo
{
    /// 
    /// App.xaml 的交互逻辑
    /// 

    public partial class App : Application
    {
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     64512  2018-04-22 13:12  DynamicSetDEmo\.vs\DynamicSetDEmo\v15\.suo

     文件          0  2018-04-22 11:28  DynamicSetDEmo\.vs\DynamicSetDEmo\v15\Server\sqlite3\db.lock

     文件     688128  2018-04-22 13:12  DynamicSetDEmo\.vs\DynamicSetDEmo\v15\Server\sqlite3\storage.ide

     文件        189  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\App.config

     文件        382  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\App.xaml

     文件        341  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\App.xaml.cs

     文件      21504  2018-04-22 13:00  DynamicSetDEmo\DynamicSetDEmo\bin\Debug\DynamicSetDEmo.exe

     文件        189  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\bin\Debug\DynamicSetDEmo.exe.config

     文件      22016  2018-04-22 13:00  DynamicSetDEmo\DynamicSetDEmo\bin\Debug\DynamicSetDEmo.pdb

     文件       4150  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\DynamicSetDEmo.csproj

     文件       3273  2018-04-22 12:57  DynamicSetDEmo\DynamicSetDEmo\MainWindow.xaml

     文件       6555  2018-04-22 13:02  DynamicSetDEmo\DynamicSetDEmo\MainWindow.xaml.cs

     文件       2322  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\Properties\AssemblyInfo.cs

     文件       2841  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\Properties\Resources.Designer.cs

     文件       5612  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\Properties\Resources.resx

     文件       1101  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\Properties\Settings.Designer.cs

     文件        201  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\Properties\Settings.settings

     文件       1141  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo.sln

     目录          0  2018-04-22 13:12  DynamicSetDEmo\.vs\DynamicSetDEmo\v15\Server\sqlite3

     目录          0  2018-04-22 11:28  DynamicSetDEmo\.vs\DynamicSetDEmo\v15\Server

     目录          0  2018-04-22 11:28  DynamicSetDEmo\.vs\DynamicSetDEmo\v15

     目录          0  2018-04-22 12:57  DynamicSetDEmo\DynamicSetDEmo\bin\Debug

     目录          0  2018-04-22 13:12  DynamicSetDEmo\DynamicSetDEmo\obj\Debug

     目录          0  2018-04-22 11:28  DynamicSetDEmo\.vs\DynamicSetDEmo

     目录          0  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\bin

     目录          0  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\obj

     目录          0  2018-04-22 11:28  DynamicSetDEmo\DynamicSetDEmo\Properties

    ...D.H.         0  2018-04-22 11:28  DynamicSetDEmo\.vs

     目录          0  2018-04-22 13:02  DynamicSetDEmo\DynamicSetDEmo

     目录          0  2018-04-22 11:28  DynamicSetDEmo

............此处省略3个文件信息

评论

共有 条评论