5.1 WPF路由事件以及文本样式

一、路由事件

WPF中存在一种路由事件(routed event),该事件将发送到包含该控件所在层次的所有控件,如果不希望继续向更高的方向传递,只要设置e.Handled = true即可。

这种从本控件-->父控件->父的父控件的事件,成为冒泡事件(bubbling event)。

路由方向也可以反向,比如从父控件->子控件->子控件的子控件。这种事件成为下钻事件(tunneling event),下钻事件都是使用Preview作为前缀。

举例说明:

1.1 对于一个grid中的button按钮,选择该按钮后按下某个按键(除了回车+空格外),则触发顺序为:

window_PreviewKeyDown --> Grid_PreviewKeyDown ->Button_PreviewKeyDown

->Button_KeyDown--> Grid_KeyDown--> window_KeyDown

可见PreviewKeyDown是下钻事件;而KeyDown是冒泡事件

1.2 如果增加 e.Handled = true,见下面代码,则触发顺序为:

window_PreviewKeyDown --> Grid_PreviewKeyDown,后面命令就不执行了

private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)

{

MessageBox.Show("grid - previewkeydown");

e.Handled = true;

}

二、作用于按钮的样式

2.1 如果所有按钮需要同样的样式,则可以使用如下(TargetType="Button")代码:

XML 复制代码
 <Window.Resources>
      <Style TargetType="Button">
          <Setter Property="Background" Value="Red"/>
          <Setter Property="FontSize" Value="15"/>
      </Style>
  </Window.Resources>

2.2 给样式命名(x:Key),然后应用到某些控件上(Style="{StaticResource greenButtonStyle}" )

XML 复制代码
 <Window.Resources>
     <Style TargetType="Button">
         <Setter Property="Background" Value="DarkGray"/>
         <Setter Property="FontSize" Value="15"/>
     </Style>
     <Style x:Key="greenButtonStyle" TargetType="Button">
         <Setter Property="Background" Value="Green"/>
         <Setter Property="FontSize" Value="15"/>
     </Style>
 </Window.Resources>

 <Button x:Name="btnFile" Style="{StaticResource greenButtonStyle}"   />

3.使用BasedOn来利用自定义的某类控件的格式

XML 复制代码
<Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="DarkGray"/>
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="Height" Value="50"/>
        </Style>
        <Style x:Key="greenButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Green"/>     
        </Style>
        <Style x:Key="yellowButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Yellow"/>
        </Style>
    </Window.Resources>
  1. 公共资源可以放置到:资源字典中:

这样每个窗口都可以使用这些公用的资源字典。

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary Source="/Style/MyButtonStyle.xaml"/>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

5.对控件本身进行样式设定

XML 复制代码
<Button x:Name="btnFile"  Width="50" HorizontalAlignment="Left"  Grid.Column="0" Grid.Row="0" Margin="0,0,0,0  " Click="btnFile_Click">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Border Background="White" BorderThickness="4" BorderBrush="Black" CornerRadius="10" >
                <ContentPresenter Content="文件" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>
相关推荐
程序员黑豆10 分钟前
Java类型推断完全指南:从var到菱形运算符,掌握使用限制与最佳实践
java·前端·ai编程
To_OC42 分钟前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
GreenTea1 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
浮生望1 小时前
前端API工程化:用 Mock 数据与 Axios 配置实现独立于后端的并行开发
前端
万少2 小时前
给 DeepSeek Harness 装个"应用商店":一条命令,595 个插件随你逛
前端·javascript·后端
波波0072 小时前
C# 15 重磅新特性: 带标签 break 与 continue:重新定义嵌套循环控制流
服务器·前端·c#
Brown.alexis3 小时前
es6知识点1-自备使用
前端·ecmascript·es6
小爬的老粉丝4 小时前
JavaScript 纯前端预览 WPS:先识别容器,再路由解析器
前端·javascript·wps
计算机魔术师4 小时前
新兴多智能体系统的模式与问题
前端
用户059540174465 小时前
AI Agent 上下文污染踩坑实录:用 pytest + Redis 揪出 3 类记忆串号 bug,排查 6 小时
前端·css