一个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. 致青春——IT之路

    我的IT青春献给了笔试.面试.人事. 笔试 如果去问一个学生最怕的是什么,或许是考试.参加过高考的都知道,高三过的是什么日子,三天一大考,一天一小考. 当时没觉得什么,因为已经麻木了. 走入职场依然要 ...

  2. A*算法&博弈树α-β剪枝

    A*算法&博弈树α-β剪枝 A*算法/博弈树 前阵子考试学了A*算法.博弈树和回溯,自己真是愚蠢至极,根本没就搞明白这些,所以对于这些算法问道的话就不能说清楚,也记不住,所以才有了这篇笔记.在 ...

  3. VS2012下基于Glut OpenGL glScissor示例程序:

    剪裁测试用于限制绘制区域.我们可以指定一个矩形的剪裁窗口,当启用剪裁测试后,只有在这个窗口之内的像素才能被绘制,其它像素则会被丢弃.换句话说,无论怎么绘制,剪裁窗口以外的像素将不会被修改.有的朋友可能 ...

  4. ubuntu下vsftpd配置

    网上的文章好难懂啊..只想要简单粗暴,弄好能用就行啊,复杂的以后研究不行吗...折腾好久,其实弄出来能用不就这么点内容吗... 本文在Ubuntu Server 14.04 amd64系统测试. 安装 ...

  5. 使用 IDEA 创建 Maven Web 项目 (四)- 让 WEB 应用跑起来

    在 IDEA 中配置 Tomcat 单击 IDEA 工具栏上的 Edit Configurations... (在一个下拉框中),弹出 Run/Debug Configurations 对话框. 单击 ...

  6. 模拟Vue之数据驱动5

    一.前言 在"模拟Vue之数据驱动4"中,我们实现了push.pop等数组变异方法. 但是,在随笔末尾我们提到,当pop.sort这些方法触发后,该怎么办呢?因为其实,它们并没有往 ...

  7. 【C语言】字符串模块

    一.字符串简介 * 在Java中,一个字符串可以用String类型来存储 String s = "MJ"; C语言中没有String这种类型.其实字符串就是字符序列,由多个字符组成 ...

  8. js处理层级数据结构的一些总结

    开发者对复杂的数据结构的处理能力也是体现开发者水平的一个度量吧...最近发现自己对一些嵌套数据结构.层级数据结构的处理能力不大足...经常被这些把自己绕晕...严重影响开发效率...就稍微低总结了一下 ...

  9. Robot Framework使用技巧

    1.变量的使用 变量可以在命令行中设置,个别变量设置使用--variable (-v)选项,变量文件的选择使用--variablefile (-V)选项.通过命令行设置的变量是全局变量,对其所有执行的 ...

  10. WIN10使用管理员权限运行VS2013

    学习WCF时出现报错-- 其他信息: HTTP 无法注册 URL http://+:8083/User/.进程不具有此命名空间的访问权限(有关详细信息,请参见 http://go.microsoft. ...