一个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添加鼠标点击响应事件的更多相关文章

  1. 为Textview里面的ImageSpan添加点击响应事件

    对于图文混排的TextView,用户在浏览到里面的图片的时候,往往有点击图片preview大图或者preview之后保存图片的需求,这就需要为Textview里面的ImageSpan设置点击响应事件. ...

  2. WPF之路二: button添加背景图片点击后图片闪烁问题

    在为button添加背景图片的时候,点击后发现图片闪烁,我们仔细观察,其实Button不仅仅只是在点击后会闪烁,在其通过点击或按Tab键获得焦点后都会闪烁,而通过点击其他按钮或通过按Tab键让Butt ...

  3. Qt窗口添加鼠标移动拖拽事件

    1. .h文件中添加 private:    QPoint dragPosition; 2. 在cpp文件中重写鼠标点击和拖拽函数 void ShapeWidget::mousePressEvent( ...

  4. cocos2dx 3.x(定时器或延时动作自动调用button的点击响应事件)实现自动内测

    // // ATTGamePoker.hpp // MalaGame // // Created by work on 2016/11/09. // // #ifndef ATTGamePoker_h ...

  5. WPF 之 style文件的引用

    总结一下WPF中Style样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...

  6. wpf研究之道——自定义Button控件

    我们知道WPF中普通的按钮,长得丑,所以自定义按钮,在所难免.我们给按钮添加 MoveBrush,EnterBrush两把刷子,其实就是鼠标经过和鼠标按下的效果.只不过这不是普通的刷子,而是带图片的I ...

  7. WPF 中style文件的引用

    原文:WPF 中style文件的引用 总结一下WPF中Style样式的引用方法: 一,内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment. ...

  8. WPF 样式Style

    一:样式基础 如果我们的程序有三个这样的按键,一般我们会这样写 <StackPanel> <!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为 ...

  9. WPF整理-Style

    "Consistency in a user interface is an important trait; there are many facets of consistency,   ...

随机推荐

  1. springMVC3学习(一)--框架搭建

    由于项目需要,学习下springMVC,在此简单记录一下. 如有十万个为什么,暂且忽略,待以后研究. 本人是基于3.1.1版本开发,如遇jar包版本冲突等其他问题,概不负责. 下载地址:上传此zip资 ...

  2. ios搭建开发环境

    ios搭建开发环境 好久就想试水IOS开发了,由于开发环境限制,一直局限于理论和虚拟机,近来入手了MacBook Pro,也来尝尝鲜,笔者也是现学现总结,如果有不足,请指正. IOS开发必备MAC O ...

  3. CC.NET-自动化发布时 Web.config 文件维护

    [Hello CC.NET]自动化发布时 Web.config 文件维护   在 <[Hello CC.NET]CC.NET 实现自动化集成> 的 HellowWorld 中经实现: 1. ...

  4. COFF/PE文件结构

    COFF/PE文件结构 原创 C++应用程序在Windows下的编译.链接(二)COFF/PE文件结构 2.1概述 在windows操作系统下,可执行文件的存储格式是PE格式:在Linux操作系统下, ...

  5. 构建一个真实的应用电子商务SportsStore(十)

    构建一个真实的应用电子商务SportsStore(十) 我们现在还需要为管理员提供一个途径,使他能方便的管理网站的商品目录,这也是所有网站都需要的功能,常用到了几乎所有开发人员都要开发这种功能的地步, ...

  6. python 提供INI配置文件的操作 ConfigParser

    原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...

  7. Android---控制设备照相机

    本文译自:http://developer.android.com/training/camera/cameradirect.html 本文中,我们讨论如何使用框架API来直接的控制照相机硬件. 直接 ...

  8. .Net程序员学用Oracle系列(9):系统函数(上)

    <.Net程序员学用Oracle系列:导航目录> 本文大纲 1.字符函数 1.1.字符函数简介 1.2.语法说明及案例 2.数字函数 2.1.数字函数简介 2.2.语法说明及案例 3.日期 ...

  9. oc之封装与类之间的关系

    1. 面向对象的特征-封装? 封装: 现实生活中的封装: 将很多的小东西 塞在1个大口袋里面. 好处: a. 对外部屏蔽. b. 方便管理. 代码的封装: 函数/方法 就是1种封装的体现: 将一段代码 ...

  10. 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView

    [源码下载] 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView 作者:webabcd ...