原文: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 为资源字典 添加事件响应的后台类的更多相关文章

  1. WPF之资源字典zz

    最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记 ...

  2. WPF合并资源字典

    1.合并多个外部资源字典成为本地字典 示例代码 <Page.Resources> <ResourceDictionary> <ResourceDictionary.Mer ...

  3. WPF使用资源字典组织资源

    转载:http://blog.163.com/wangzhenguo2005@126/blog/static/371405262010111413321728/     首先在解决方案资源管理器中添加 ...

  4. wpf 切换资源字典的2中方式

    var _1200RDUri = new Uri(String.Format(@"/aa;Component/Themes/1200Theme.xaml"), UriKind.Re ...

  5. WPF 遍历资源字典中的控件

    object obItem=this.FindResource("canvasdt"); if (obItem is System.Windows.DataTemplate) { ...

  6. (转载)资源字典(Pro WPF 学习)

    原地址:http://www.cnblogs.com/yxhq/archive/2012/07/09/2582508.html 1.创建资源字典 下面是一个资源字典(AppBrushes.xaml), ...

  7. WPF资源字典

    如果相同的资源可用于不同的应用程序,把资源放在一个资源字典中就比较有效. 新建一个资源字典文件Dictionary1.xaml <ResourceDictionary xmlns="h ...

  8. WPF 界面实现多语言支持 中英文切换 动态加载资源字典

    1.使用资源字典,首先新建两个字典文件en-us.xaml.zh-cn.xaml.定义中英文的字符串在这里面[注意:添加xmlns:s="clr-namespace:System;assem ...

  9. WPF(MVVM) 利用资源字典实现中英文动态切换

    1.首先新建两个字典文件en-us.xaml.zh-cn.xaml.定义中英文的字符串在这里面. 2.将两个资源字典添加到App.xaml中,这里注意下,因为两个字典中有同样字符,如果没有动态更改,默 ...

随机推荐

  1. Mybatis中sql语句中的in查询,一定要判断null的情况

    不严谨的写法,可能会报错:in (),这种情况不符合mysql的语法. select from loanwhere LOAN_ID in <foreach item="item&quo ...

  2. [Ramda] Count Words in a String with Ramda's countBy and invert

    You can really unlock the power of ramda (and functional programming in general) when you combine fu ...

  3. Jquery前端分页插件pagination同步加载和异步加载

    上一篇文章介绍了Jquery前端分页插件pagination的基本使用方法和使用案例,大致原理就是一次性加载所有的数据再分页.https://www.jianshu.com/p/a1b8b1db025 ...

  4. MongoDB Shell 经常使用操作

    数组查询 数组查询 MongoDB 中有子文档的概念.一个文档中能方便的嵌入子文档,这与关系性数据库有着明显的不同,在查询时,语法有一些注意点. 样例代码,假如我们的一个集合(tests)中存在标签键 ...

  5. Log4erl

    http://developerworks.github.io/2011/05/16/erlang-use-log4erl/ Erlang 使用Log4erl all = debug < inf ...

  6. 【9005】最短网络agrinet

    Time Limit: 1 second Memory Limit: 256 MB 问题描述 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的 ...

  7. hbase 配置高可用hmaster

    1.先停掉hbase bin/stop-hbase.sh 2.在hbase的conf目录下创建 backup-masters 添加hadoop003 3.分发 4.重新启动hbase并查看 bin/s ...

  8. Eclipse迅速执行:Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: Java heap space

    问题叙述性说明: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space 问题原因: 程序中对 ...

  9. KDE 邀请用户测试 Plasma Mobile 的首个专用 ISO 镜像(可以考虑做一个极客。。。)

    KDE 项目依旧在继续改进智能手机.平板电脑和其他移动设备的 Plasma Mobile 用户界面,并于近日发布了一个 ISO 镜像,邀请社区的尝鲜用户进行测试. 他们曾承诺在 2018 年年底之前, ...

  10. 在 Oracle 中新建 SDE 用户

    --1.创建用户(SDE)和密码(SDE) CREATE USER SDE IDENTIFIED BY SDE --2.创建表空间(SDE) CREATE TABLESPACE SDE DATAFIL ...