[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, ...
随机推荐
- 仿腾讯微博的一个弹出框 v0.1 beta
仿腾讯微博的一个弹出框 v0.1 beta 代码写的不太好,新手请大家海涵,只为博君一笑,勿放在心上. 写在这里留作纪念,也许以后用的到. 效果 CSS .prompt{ position: ab ...
- DDNS client on a Linux machine
Using this tool -> inadyn apt-get install inadyn -y Usage: https://github.com/troglobit/inadyn#ex ...
- TOGAF架构内容框架之内容元模型(上)
TOGAF架构内容框架之内容元模型(上) 2. 内容元模型(Content Metamodel) 在TOGAF的眼中,企业架构是以一系列架构构建块为基础的,并将目录.矩阵和图形作为其具体展现方式.如果 ...
- 【OpenMesh】使用迭代器和循环机
原文出处: http://openmesh.org/Documentation/OpenMesh-Doc-Latest/tutorial.html 这个例子展现: 如何使用迭代器 如何使用循环机 这个 ...
- go: GOPATH entry is relative; must be absolute path: "".
安装:vscode-go出现以下提示: go: GOPATH entry is relative; must be absolute path: "".Run 'go help g ...
- JavaScript 中 if 条件判断
在JS中,If 除了能够判断bool的真假外,还能够判断一个变量是否有值. 下面的例子说明了JS中If的判断逻辑: 变量值 true '1' 1 '0' 'null' 2 '2' false 0 n ...
- Android开发华为手机无法看log日志解决方法
Android开发华为手机无法看log日志解决方法 上班的时候,由于开发工具由Eclipse改成Android Studio后,原本的华为手机突然无法查看崩溃日志了,大家都知道,若是无法查看日志要它毛 ...
- Laravel5.3 流程粗粒度分析之bootstrap
从laravel入口文件index里面外面不难定位到Illuminate\Foundation\Http\Kernel的handle方法,同样也不难发现handle方法的核心是 $response = ...
- Struts入门(二) 配置文件的讲解
上一章我们演示了Struts项目的搭建 可以看到里面有几个重要的配置文件 下面我们来说明一下这3个配置文件 1.web.xml 2.strtus.xml 3.struts.properties 1 ...
- JavaSE——UDP协议网络编程(一)
UDP协议基础: UDP协议是英文UserDatagramProtocol的缩写,即用户数据报协议,主要用来支持那些需要在计算机之间传输数据的网络应用.包括网络视频会议系统在内的众多的客户/服务器模式 ...