一个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. Google开源的Deep-Learning项目word2vec

    用中文把玩Google开源的Deep-Learning项目word2vec   google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算te ...

  2. window.parent与window.opener的区别

    有这样一个需求,弹出一个新窗口 并从该新页面的select选择框中选择需要的类别,再返回到之前的父窗口页面的某个文本框中.这里就要用到window.parent和window.opener 如题两种方 ...

  3. 使用JAVA进行MD5加密后所遇到的一些问题

    前言:这几天在研究apache shiro如何使用,这好用到了给密码加密的地方,就碰巧研究了下java的MD5加密是如何实现的,下面记录下我遇到的一些小问题. 使用java进行MD5加密非常的简单,代 ...

  4. 开发框架(WinForm)3

    我的开发框架(WinForm)3 今天继续给大家介绍核心库的IOC的使用,在我的框架里,IOC使用的比较简单,主要是用于解除模块间的耦合和实例化接口. 1.接口说明,IocContainer接口比较简 ...

  5. 一键搭键php网站环境的系统

    QzzmServer v2.0正式版发布 首先,感谢网友的热情的测评及反馈,现QzzmServer 2.0正式版已发布.有些朋友反馈制作一个服务器专用版本,在下已将此列入计划中,敬请大家耐心等候. Q ...

  6. FastDFS接口API文档说明

    FastDFS接口API文档说明 时间:2012-03-17 来源:R9IT 作者:R9传奇 一.命令行的上传: 上传命令1. /usr/local/bin/fdfs_upload_file conf ...

  7. MSSQL Server查询优化方法(转)

    核心提示:查询速度慢的原因很多,常见如下几种 查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3. ...

  8. static类成员(变量和函数)

    0. 使用背景 对于特定类类型的全体对象而言,访问一个全局对象有时是必要的.也许,在程序的任意点需要统计已创建的特定类类型对象的数量:或者,全局对象可能是指向类的错误处理例程的一个指针:或者,它是指向 ...

  9. C语言与汇编“硬在哪里”——什么是面向硬件?

    Jack:为什么说C/C++语言是偏向硬件的语言呢? 我:这是把C与java等无指针/引用类编程语言相比较而得出的结论.因为java在j2ee的框架下,写的代码仅仅是逻辑,本质上和写shell脚本没啥 ...

  10. [ios2][转]iOS摇动检测 (UIAccelerometer)

    加速计(UIAccelerometer)是一个单例模式的类,所以需要通过方法sharedAccelerometer获取其唯一的实例. 加速计需要设置的主要有两个: 一个是设置其代理,用以执行获取加速计 ...