[WPF] 为Style 里的button添加鼠标点击响应事件
一个TabControl, 用的是PagedTabControl style, 在style中有个button, button在style里已经写了click事件,但是现在还需要加上一段功能,就是在响应事件之前对界面作一下判断。该怎么办呢?先看代码:
1. 控件XAML部分代码(位于文件form_loadatorigin.xaml):
<!-- Form Body -->
<TabControl x:Name="formLoadUnload"
Style="{StaticResource PagedTabControl}"
Tag="" HorizontalAlignment="Stretch"
Grid.Row="0" Grid.RowSpan="2"
Grid.IsSharedSizeScope="True"
> <!-- Page 1 -->
<TabItem Name="LoadUnloadPage1" Foreground="Black">
<!-- 此处为Page1页面 略去-->
</TabItem> <!-- Page 2 -->
<TabItem Name="LoadUnloadPage2" Foreground="Black">
<!-- 此处为Page2页面 略去-->
</TabItem>
</TabControl>
2. Style PagedTabControl代码:
<Style x:Key="PagedTabControl" TargetType="{x:Type TabControl}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid KeyboardNavigation.TabNavigation="Local">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TabPanel Name="PagedHeaderPanel"
Grid.Row="0"
Grid.ColumnSpan="2"
Panel.ZIndex="0"
Margin="2,0,4,0"
KeyboardNavigation.TabIndex="1"
/>
<StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal" Panel.ZIndex="1" Margin="0,0,50,0"
KeyboardNavigation.TabIndex="1">
<Button x:Name="PreviousPageButton"
Grid.Column="0"
Background="White">
<Button.Content>
<Image Source="/Common.WPF.Assets;component/Images/btn_left.png"/>
</Button.Content>
<Button.Style>
<Style BasedOn="{StaticResource RoundButtonStyle}" TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent},Path=SelectedIndex}" Value="0">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<Int32Animation
Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=''}"
Storyboard.TargetProperty="SelectedIndex"
By="-1" Duration="0:0:.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<TextBlock VerticalAlignment="Center" Margin="10,0,10,0" Style="{StaticResource NormalText19Style}" HorizontalAlignment="Center" TextAlignment="Center" >
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1} of {2}">
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Tag"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="SelectedIndex" Converter="{StaticResource ZeroBasedIndexConverter}"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Items.Count"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<Button x:Name="NextPageButton"
Background="White">
<Button.Content>
<Image Source="/Common.WPF.Assets;component/Images/btn_right.png"/>
</Button.Content>
<Button.Style>
<Style BasedOn="{StaticResource RoundButtonStyle}" TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent},Path=SelectedIndex}" Value="Items.Count">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<Int32Animation
Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=''}"
Storyboard.TargetProperty="SelectedIndex"
By="1" Duration="0:0:.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
<TabPanel
Name="HeaderPanel"
Grid.Row="0"
Panel.ZIndex="1"
Height="0"
IsItemsHost="True">
</TabPanel>
<Border
Name="Border"
Grid.Row="1"
Grid.ColumnSpan="2"
BorderThickness="1"
CornerRadius="2"
KeyboardNavigation.TabNavigation="Local"
KeyboardNavigation.DirectionalNavigation="Contained"
KeyboardNavigation.TabIndex="2">
<ContentPresenter
Name="PART_SelectedContentHost"
Margin="4"
ContentSource="SelectedContent" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我们需要NextPageButton添加鼠标点击,下面就是在控件的实现文件里添加的代码:
3. 在form_loadatorigin.xaml.cs文件里的实现代码:
public form_loadatorigin()
{
InitializeComponent(); this.Loaded += new RoutedEventHandler(form_loadatorigin_Loaded);
} private void form_loadatorigin_Loaded(object sender, RoutedEventArgs e)
{
if (!IsFirstLoaded)
{
IsFirstLoaded = true;
formLoadUnload.SelectionChanged += new SelectionChangedEventHandler(formLoadUnload_SelectionChanged); DependencyObject d1 = VisualTreeHelper.GetChild(formLoadUnload, );
m_NextPageButton = LogicalTreeHelper.FindLogicalNode(d1, "NextPageButton") as Button;
m_NextPageButton.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(m_NextPageButton_PreviewMouseLeftButtonDown);
}
} void m_NextPageButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
if (ValidateAndShowErrors())
{
m_NextPageButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, m_NextPageButton));
}
} private Button m_NextPageButton;
private bool IsFirstLoaded = false;
注意:
1. m_NextPageButton必须为成员变量,如果改成局部变量,则添加事件不会起作用。至于原因,还没有找到为什么。
2. 此为抛砖引玉之技法,如有更好的方法,还望高手不吝赐教!
[WPF] 为Style 里的button添加鼠标点击响应事件的更多相关文章
- 为Textview里面的ImageSpan添加点击响应事件
对于图文混排的TextView,用户在浏览到里面的图片的时候,往往有点击图片preview大图或者preview之后保存图片的需求,这就需要为Textview里面的ImageSpan设置点击响应事件. ...
- WPF之路二: button添加背景图片点击后图片闪烁问题
在为button添加背景图片的时候,点击后发现图片闪烁,我们仔细观察,其实Button不仅仅只是在点击后会闪烁,在其通过点击或按Tab键获得焦点后都会闪烁,而通过点击其他按钮或通过按Tab键让Butt ...
- Qt窗口添加鼠标移动拖拽事件
1. .h文件中添加 private: QPoint dragPosition; 2. 在cpp文件中重写鼠标点击和拖拽函数 void ShapeWidget::mousePressEvent( ...
- cocos2dx 3.x(定时器或延时动作自动调用button的点击响应事件)实现自动内测
// // ATTGamePoker.hpp // MalaGame // // Created by work on 2016/11/09. // // #ifndef ATTGamePoker_h ...
- WPF 之 style文件的引用
总结一下WPF中Style样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...
- wpf研究之道——自定义Button控件
我们知道WPF中普通的按钮,长得丑,所以自定义按钮,在所难免.我们给按钮添加 MoveBrush,EnterBrush两把刷子,其实就是鼠标经过和鼠标按下的效果.只不过这不是普通的刷子,而是带图片的I ...
- WPF 中style文件的引用
原文:WPF 中style文件的引用 总结一下WPF中Style样式的引用方法: 一,内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment. ...
- WPF 样式Style
一:样式基础 如果我们的程序有三个这样的按键,一般我们会这样写 <StackPanel> <!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为 ...
- WPF整理-Style
"Consistency in a user interface is an important trait; there are many facets of consistency, ...
随机推荐
- 论公司spring的滥用
这个公司每个项目用不同的一套开发框架,实在忍不住拿一个出来说说事.
- ckeditor:复制内容到ckeditor时,只保留文本,忽略其样式解决方法
打开ckeditor 包下的config.js,在 CKEDITOR.editorConfig= function(config){...}添加一句配置: config.forcePasteAsPla ...
- TCP连接的建立与终止
TCP/IP详解学习笔记(13)-- TCP连接的建立与终止 1.TCP连接的建立 设主机B运行一个服务器进程,它先发出一个被动打开命令,告诉它的TCP要准备接收客户进程的连续请 ...
- Centos 64位 Install certificate on apache 即走https协议
Centos 64位 Install certificate on apache 即走https协议 一: 先要apache 请求ssl证书的csr 一下是步骤: 重要注意事项 An Importan ...
- python实现基于CGI的Web应用
python实现基于CGI的Web应用 本文用一个“网上书店”的web应用示例,简要介绍如何用Python实现基于CGI标准的Web应用,介绍python的cgi模块.cigtb模块对编写CGI脚本提 ...
- 方法输出C++输出斐波那契数列的几种方法
PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 定义: 斐波那契数列指的是这样一个数列:0, 1, 1, 2, 3, 5, ...
- .Net程序员学用Oracle系列(1):导航目录
本人从事基于 Oracle 的 .Net 企业级开发近三年,在此之前学习和使用的都是 (MS)SQL Server.未曾系统的了解过 Oracle,所以长时间感到各种不习惯.不方便.怪异和不解,常会遇 ...
- jvm实战-基本类型占多少内存
jvm内存占用模型 对象的内存结构 对象头 Header 包含两部分数据Mark Word和Kclass: Mark Word:存储对象自身的运行时数据,如hashCode.GC分代年龄.锁状态标志. ...
- windows下练习linux shell
<---开始学习linux---记录一下---路漫漫其修远兮---加油吧---萌萌达> 使用软件:Cygwin 下载地址(免安装版):链接: http://pan.baidu.com/s ...
- 【Zookeeper】源码分析之持久化--FileSnap
一.前言 前篇博文已经分析了FileTxnLog的源码,现在接着分析持久化中的FileSnap,其主要提供了快照相应的接口. 二.SnapShot源码分析 SnapShot是FileTxnLog的父类 ...