WPF 为资源字典 添加事件响应的后台类
前言,有许多同学在写WPF程序时在资源字典里加入了其它控件,但又想写事件来控制这个控件,但是资源字典没有CS文件,不像窗体XAML还有一个后台的CS文件,怎么办呢?
在工作时也遇到了这个问题,现在把它分享出来
比如说我们现在要写一个TabControl控件,在TabItem中有一个关闭按钮或其它按钮,这个按钮要能响应某个事件。
现在开始写资源字典里的 TabItem的样式,代码如下
<Style x:Key="TIStyle" TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="layout" BorderBrush="Gray" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}"
CornerRadius="3" Margin="2,0,2,0">
<Grid Height="20">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<TextBlock TextAlignment="Center" Text="{TemplateBinding Header}" Grid.Column="0" Margin="4,0,3,0"
VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Content="X" Grid.Column="1" Height="8" Width="8" Margin="4,1,3,2"
Tag="{TemplateBinding Header}" Click="Button_Click"
Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Center">
<Button.Template>
<ControlTemplate >
<Grid>
<Rectangle>
<Rectangle.Fill>
<VisualBrush>
<VisualBrush.Visual>
<Path x:Name="btnPath"
Data="M0 0L10 10M0 10L10 0" Stroke="Gray"
StrokeThickness="1"/>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="btnPath" Property="Stroke" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White"/>
<Setter TargetName="layout" Property="Margin" Value="2,0,2,-1.5"/>
</Trigger>
<Trigger Property="IsSelected" Value="false">
<Setter Property="Background" Value="LightBlue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后为资源字典建一个后台类DicEvent.cs
public partial class DicEvent : ResourceDictionary
{
public void Button_Click(object sender, RoutedEventArgs e)
{
//省去处理,如果显示,表明调用成功。
MessageBox.Show("你成功了!");
}
}
在资源字典里,添加对后台类的引用
主窗口里调用:
<Window x:Class="WpfDicButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl>
<TabItem Header="第一" Height=" 30" Style="{DynamicResource ResourceKey=TIStyle}"></TabItem> </TabControl>
</Grid>
</Window>
记住APP文件里加入资源字典
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TabControlDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
测试,小功告成
奉上DEMO 下载地址
WPF 为资源字典 添加事件响应的后台类的更多相关文章
- WPF之资源字典zz
最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记 ...
- WPF合并资源字典
1.合并多个外部资源字典成为本地字典 示例代码 <Page.Resources> <ResourceDictionary> <ResourceDictionary.Mer ...
- WPF使用资源字典组织资源
转载:http://blog.163.com/wangzhenguo2005@126/blog/static/371405262010111413321728/ 首先在解决方案资源管理器中添加 ...
- wpf 切换资源字典的2中方式
var _1200RDUri = new Uri(String.Format(@"/aa;Component/Themes/1200Theme.xaml"), UriKind.Re ...
- WPF 遍历资源字典中的控件
object obItem=this.FindResource("canvasdt"); if (obItem is System.Windows.DataTemplate) { ...
- (转载)资源字典(Pro WPF 学习)
原地址:http://www.cnblogs.com/yxhq/archive/2012/07/09/2582508.html 1.创建资源字典 下面是一个资源字典(AppBrushes.xaml), ...
- WPF资源字典
如果相同的资源可用于不同的应用程序,把资源放在一个资源字典中就比较有效. 新建一个资源字典文件Dictionary1.xaml <ResourceDictionary xmlns="h ...
- WPF 界面实现多语言支持 中英文切换 动态加载资源字典
1.使用资源字典,首先新建两个字典文件en-us.xaml.zh-cn.xaml.定义中英文的字符串在这里面[注意:添加xmlns:s="clr-namespace:System;assem ...
- WPF(MVVM) 利用资源字典实现中英文动态切换
1.首先新建两个字典文件en-us.xaml.zh-cn.xaml.定义中英文的字符串在这里面. 2.将两个资源字典添加到App.xaml中,这里注意下,因为两个字典中有同样字符,如果没有动态更改,默 ...
随机推荐
- 让Apache 和nginx支持跨域訪问
1,怎样让Apache支持跨域訪问呢? 步骤: 改动httpd.conf,windows中相应的文件夹是:C:\wamp\bin\apache\Apache2.4.4\conf\httpd.conf ...
- 【codeforces 754B】 Ilya and tic-tac-toe game
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- iOS9.0 生成证书流程一(非推送)
1.首先进入网址 http://developer.apple.com 2.第二登入,点击
- 【u204】高级砝码称重
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 现有n个砝码,重量分别为a1,a2,a3,--,an,在去掉m个砝码后,问最多能称量出多少不同的重量( ...
- 解析字典包含关键字比如ID,description等,MJExtension 框架 不能直接设置变量与其同名。
@property (nonatomic,strong) NSString *descrip; //设置变量名 _DataReceived=(NSMutableArray *)[HZnewsmess ...
- Java堆/栈/常量池以及String的详细详解(转)------经典易懂系统
一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...
- 项目构建之maven篇:6.生命周期与插件
项目生命周期 清理 初始化 编译 測试 打包 部署 三套生命周期 1.clean pre-clean 运行一些须要在clean之前完毕的工作 clean 移除全部上一次构建生成的文件 post-cle ...
- htmlunit 模拟登录 无验证码
1.模拟登录csdn,最开始的时候使用的是httpclient,网上的所有模拟登录csdn的版本都是找到lt/execution/event_id.连同用户名及密码 一起发送即可,但是目前的csdn的 ...
- 在CentOS系统上将deb包转换为rpm包
转载自 http://www.heminjie.com/system/linux/1487.html deb文件格式本是ubuntu/debian系统下的安装文件,那么我想要在redhat/cento ...
- SQLite的查询优化
SQLite是个典型的嵌入式DBMS,它有很多优点,它是轻量级的,在编译之后很小,其中一个原因就是在查询优化方面比较简单,它只是运用索引机制来进行优化的,经过对SQLite的查询优化的分析以及对源代码 ...