回答編集履歴

2

テンプレートを外に

2019/10/30 03:33

投稿

Zuishin
Zuishin

スコア28669

test CHANGED
@@ -8,15 +8,25 @@
8
8
 
9
9
  ```C#
10
10
 
11
- <Label x:Name="MyLabel" Content="Mouse hover over a Label"
11
+ <Window x:Class="WpfApp1.MainWindow"
12
12
 
13
- HorizontalAlignment="Center"
13
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
14
 
15
- VerticalAlignment="Center">
15
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
16
 
17
- <Label.Template>
17
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
18
18
 
19
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
20
+
21
+ xmlns:local="clr-namespace:WpfApp1"
22
+
23
+ mc:Ignorable="d"
24
+
25
+ Title="MainWindow" Height="450" Width="800">
26
+
27
+ <Window.Resources>
28
+
19
- <ControlTemplate TargetType="{x:Type Label}">
29
+ <ControlTemplate TargetType="{x:Type Label}" x:Key="labelTemplate">
20
30
 
21
31
  <Border Background="{TemplateBinding Background}" x:Name="border">
22
32
 
@@ -40,8 +50,22 @@
40
50
 
41
51
  </ControlTemplate>
42
52
 
43
- </Label.Template>
53
+ </Window.Resources>
44
54
 
55
+ <Grid>
56
+
57
+ <Label x:Name="MyLabel" Content="Mouse hover over a Label"
58
+
59
+ Template="{StaticResource labelTemplate}"
60
+
61
+ HorizontalAlignment="Center"
62
+
63
+ VerticalAlignment="Center">
64
+
45
- </Label>
65
+ </Label>
66
+
67
+ </Grid>
68
+
69
+ </Window>
46
70
 
47
71
  ```

1

追記

2019/10/30 03:32

投稿

Zuishin
Zuishin

スコア28669

test CHANGED
@@ -3,3 +3,45 @@
3
3
 
4
4
 
5
5
  ControlTemplate.Triggers を使ってみてください。
6
+
7
+
8
+
9
+ ```C#
10
+
11
+ <Label x:Name="MyLabel" Content="Mouse hover over a Label"
12
+
13
+ HorizontalAlignment="Center"
14
+
15
+ VerticalAlignment="Center">
16
+
17
+ <Label.Template>
18
+
19
+ <ControlTemplate TargetType="{x:Type Label}">
20
+
21
+ <Border Background="{TemplateBinding Background}" x:Name="border">
22
+
23
+ <ContentPresenter/>
24
+
25
+ </Border>
26
+
27
+ <ControlTemplate.Triggers>
28
+
29
+ <Trigger Property="IsMouseOver" Value="True">
30
+
31
+ <Setter Property="FontSize" Value="30"/>
32
+
33
+ <Setter Property="Foreground" Value="Red"/>
34
+
35
+ <Setter TargetName="border" Property="Background" Value="Yellow"/>
36
+
37
+ </Trigger>
38
+
39
+ </ControlTemplate.Triggers>
40
+
41
+ </ControlTemplate>
42
+
43
+ </Label.Template>
44
+
45
+ </Label>
46
+
47
+ ```