1. 如上图,所示,此时当鼠标移入蓝色框内除文字部分,整个TabItem是没反应的

经过查看代码可以看到:

将图标中的VerticalAlignment="Center"和HorizontalAlignment="Center"删掉后,就能达到如下效果:

TabItem的可选区域明显恢复到正常情况。

完整的代码如下:

1. MainWindow.xaml

 <Window x:Class="TabItemDemo20160329.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl TabStripPlacement="Left">
<TabItem Header="第一项"></TabItem>
<TabItem Header="第二项"></TabItem>
<TabItem Header="第三项"></TabItem>
<TabItem Header="第四项"></TabItem>
<TabItem Header="第五项"></TabItem>
<TabItem Header="第六项"></TabItem>
</TabControl>
</Grid>
</Window>

2. MainWindow.xaml.cs ,该文件其实就是默认系统生成的

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace TabItemDemo20160329
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

3. TabItemStyle.xaml 样式文件:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TabItem}">
<Setter Property="Width" Value="200"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="true" Background="{x:Null}" x:Name="tabItemGrid" Cursor="Hand">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="43*"/>
<ColumnDefinition Width="157*"/>
</Grid.ColumnDefinitions>
<Border x:Name="border" BorderBrush="{x:Null}" Background="{TemplateBinding Background}">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border> <Label x:Name="lbl" Foreground="White" Grid.Column="1" FontSize="16" FontFamily="Tomaha" Padding="0">
<Label.Content>
<TextBlock Margin="0,0,5,0" Text="{TemplateBinding Header}" TextWrapping="Wrap"/>
</Label.Content>
</Label>
<!--<Label x:Name="lbl" Grid.Column="1" Content="{TemplateBinding Header}"/>-->
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="lbl" Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="lbl" Property="Background" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

4.App.xaml 配置文件,注意是将TabItem.xaml文件引入到App中:

 <Application x:Class="TabItemDemo20160329.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TabItemStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

WPF中定义TabItem的可选区域(特别是当使用Label来呈现Header时)的更多相关文章

  1. 如何在WPF中定义窗体模板

    参考网址:https://www.cnblogs.com/chenxizhang/archive/2010/01/10/1643676.html可以在app.xaml中定义一个ControlTempl ...

  2. Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)

    原文:Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) ------------------------------ ...

  3. WPF中TimeSpan的坑

    记一次在WPF中,在将格式为“DD.HH:mm:ss”字符串转换成TimeSpan时遇到的坑 如果字符串为:DD.HH:mm:ss,转换结果正确.例如: var currentValue = &quo ...

  4. WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了

    原文:WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4 ...

  5. WPF中通过代码定义模板

    WPF中可以再XAML中定义模板,也可以通过C#代码定义模板,通过代码可能更清楚的看清其逻辑,而且代码的好处就是可以随时动态的去操作,而在XAML中定义的一般都是静态的. //控件呈现的显示内容1(这 ...

  6. 【C#】WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的

    原文:[C#]WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的 初学WPF,知道一些控件可以通过定义Style的Trigger改变要显示的样式,但是 ...

  7. JavaScript jQuery 中定义数组与操作及jquery数组操作

    首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...

  8. WPF中的数据绑定!!!

    引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx  数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...

  9. WPF中图形表示语法详解(Path之Data属性语法)ZZ

    大可山 [MSN:a3news(AT)hotmail.com] http://www.zpxp.com 萝卜鼠在线图形图像处理 ------------------------------------ ...

随机推荐

  1. D3_book 7 area

    <!-- area的例子csv使用node.js提供的 --> <!DOCTYPE html> <meta charset="utf-8"> & ...

  2. 基于注解方式@AspectJ的AOP

    启用对@AspectJ的支持 Spring默认不支持@AspectJ风格的切面声明,为了支持需要使用如下配置: <aop:aspectj-autoproxy/> 这样Spring就能发现@ ...

  3. [c# 20问] 4.Console应用获取执行路径

    一行代码可以搞定了~ static void GetAppPath() { string path = System.Reflection.Assembly.GetExecutingAssembly( ...

  4. Microsoft SQL Server 2012 管理 (1): 安装配置SQL Server 重点

    SQL Server 可以在实例,数据库,列,查询分别指定排序规则 /* Module 1 - working with Clollations */ -- 1.1 Obtain the Instan ...

  5. Alwayson--与复制的影响

    在主副本上建立复制后,复制的事务日志读取代理(log reader)不会读取尚未同步到辅助副本的日志,因为辅助副本可能在下一时刻转化成为主副本,变为新的复制发布服务器,为此需要保证复制处理的日志总慢于 ...

  6. .NET中的FileUpload控件的使用-原生JS(二)

    本篇使用原生JS进行数据传输,使用FileUpload控件上传文件,适配IE. HTML <div class="container"> <div class=& ...

  7. ueditor图片上传插件的使用

    在项目里使用到ueditor图片上传插件,以前图片上传都是直接使用js代码直接上传图片,比较麻烦,而且效率也比较低,而ueditor这款插件完美的解决了这个问题,这个是百度开发的一款富文本编辑器,在这 ...

  8. Python常用第三方模块(长期更新)

    1.keyboard #监控键盘 2.PIL#处理图片 3.operator #操作列表 4.shelve #数据存储方案 保存dat文件 5.optparse #处理命令行参数 6.configpa ...

  9. 日常一些出现bug的问题

    1.Fatal signal 4 (SIGILL), code 1, fault addr 0xca31569e in tid 8033 (r.myapplication) fault addr : ...

  10. 【2019年OCP新题】OCP题库更新出现大量新题-11

    11.Your database is in archivelog mode. You want to disable archiving for the database. Examine thes ...