回答編集履歴
2
テンプレートを外に
answer
CHANGED
@@ -3,11 +3,16 @@
|
|
3
3
|
ControlTemplate.Triggers を使ってみてください。
|
4
4
|
|
5
5
|
```C#
|
6
|
+
<Window x:Class="WpfApp1.MainWindow"
|
7
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
6
|
-
|
8
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
9
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
10
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
11
|
+
xmlns:local="clr-namespace:WpfApp1"
|
7
|
-
|
12
|
+
mc:Ignorable="d"
|
8
|
-
|
13
|
+
Title="MainWindow" Height="450" Width="800">
|
9
|
-
<
|
14
|
+
<Window.Resources>
|
10
|
-
<ControlTemplate TargetType="{x:Type Label}">
|
15
|
+
<ControlTemplate TargetType="{x:Type Label}" x:Key="labelTemplate">
|
11
16
|
<Border Background="{TemplateBinding Background}" x:Name="border">
|
12
17
|
<ContentPresenter/>
|
13
18
|
</Border>
|
@@ -19,6 +24,13 @@
|
|
19
24
|
</Trigger>
|
20
25
|
</ControlTemplate.Triggers>
|
21
26
|
</ControlTemplate>
|
22
|
-
</
|
27
|
+
</Window.Resources>
|
28
|
+
<Grid>
|
29
|
+
<Label x:Name="MyLabel" Content="Mouse hover over a Label"
|
30
|
+
Template="{StaticResource labelTemplate}"
|
31
|
+
HorizontalAlignment="Center"
|
32
|
+
VerticalAlignment="Center">
|
23
|
-
</Label>
|
33
|
+
</Label>
|
34
|
+
</Grid>
|
35
|
+
</Window>
|
24
36
|
```
|
1
追記
answer
CHANGED
@@ -1,3 +1,24 @@
|
|
1
1
|
[Style.TriggersとControlTemplate.Triggersの違い - プログラマーな日々](https://blog.jhashimoto.net/entry/20110409/1302510138)
|
2
2
|
|
3
|
-
ControlTemplate.Triggers を使ってみてください。
|
3
|
+
ControlTemplate.Triggers を使ってみてください。
|
4
|
+
|
5
|
+
```C#
|
6
|
+
<Label x:Name="MyLabel" Content="Mouse hover over a Label"
|
7
|
+
HorizontalAlignment="Center"
|
8
|
+
VerticalAlignment="Center">
|
9
|
+
<Label.Template>
|
10
|
+
<ControlTemplate TargetType="{x:Type Label}">
|
11
|
+
<Border Background="{TemplateBinding Background}" x:Name="border">
|
12
|
+
<ContentPresenter/>
|
13
|
+
</Border>
|
14
|
+
<ControlTemplate.Triggers>
|
15
|
+
<Trigger Property="IsMouseOver" Value="True">
|
16
|
+
<Setter Property="FontSize" Value="30"/>
|
17
|
+
<Setter Property="Foreground" Value="Red"/>
|
18
|
+
<Setter TargetName="border" Property="Background" Value="Yellow"/>
|
19
|
+
</Trigger>
|
20
|
+
</ControlTemplate.Triggers>
|
21
|
+
</ControlTemplate>
|
22
|
+
</Label.Template>
|
23
|
+
</Label>
|
24
|
+
```
|