回答編集履歴

1

ボタンの青くなる処理削除、画像とグレーアウトがずれる原因を多分修正

2017/04/18 04:38

投稿

doubutweet
doubutweet

スコア141

test CHANGED
@@ -1,4 +1,4 @@
1
- こんな感じでか?
1
+ こんな感じでしょうか?
2
2
 
3
3
  ```
4
4
 
@@ -18,14 +18,148 @@
18
18
 
19
19
  Title="MainWindow" Height="350" Width="525">
20
20
 
21
-
22
-
23
-
24
-
25
21
  <Window.Resources>
26
22
 
27
23
  <!-- ボタンに表示する画像 -->
28
24
 
25
+ <BitmapImage x:Key="Image_Button_ON" UriSource="./Resources/user.png" />
26
+
27
+
28
+
29
+ <!-- ボタンのクリックをトリガーとしてグレーアウト用のボーダーの色を変更する -->
30
+
31
+ <Style x:Key="ClickButton" TargetType="Border">
32
+
33
+ <Style.Triggers>
34
+
35
+ <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsPressed}" Value="True" >
36
+
37
+ <Setter Property="Background" Value="Black" />
38
+
39
+ <Setter Property="Opacity" Value="0.5" />
40
+
41
+ </DataTrigger>
42
+
43
+ </Style.Triggers>
44
+
45
+ </Style>
46
+
47
+ </Window.Resources>
48
+
49
+
50
+
51
+ <Grid>
52
+
53
+ <Button>
54
+
55
+ <Button.Style>
56
+
57
+ <Style TargetType="Button">
58
+
59
+ <Setter Property="Template">
60
+
61
+ <Setter.Value>
62
+
63
+ <ControlTemplate TargetType="{x:Type Button}">
64
+
65
+ <!-- ボタンのデフォルトテンプレートの背景、ボーダーにする -->
66
+
67
+ <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
68
+
69
+
70
+
71
+ <!-- ボタンの子要素のコンテナ -->
72
+
73
+ <!-- HorizontalAlignmentとVerticalAlignmentをCenterにすることで画像とグレーアウトのサイズを統一し間延びするのを防ぐ -->
74
+
75
+ <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
76
+
77
+ <!-- 画像 -->
78
+
79
+ <Image Source="{StaticResource Image_Button_ON}" />
80
+
81
+
82
+
83
+ <!-- グレーアウト用 -->
84
+
85
+ <Border Style="{StaticResource ClickButton}" >
86
+
87
+ <!-- OpacityMaskで画像領域のみ色を変更する -->
88
+
89
+ <Border.OpacityMask>
90
+
91
+ <ImageBrush ImageSource="{StaticResource Image_Button_ON}" />
92
+
93
+ </Border.OpacityMask>
94
+
95
+ </Border>
96
+
97
+ </Grid>
98
+
99
+ </Border>
100
+
101
+ </ControlTemplate>
102
+
103
+ </Setter.Value>
104
+
105
+ </Setter>
106
+
107
+ </Style>
108
+
109
+ </Button.Style>
110
+
111
+ </Button>
112
+
113
+ </Grid>
114
+
115
+ </Window>
116
+
117
+ ```
118
+
119
+  
120
+
121
+  
122
+
123
+  
124
+
125
+ ## ボタンから青くなる処理を削除する参考
126
+
127
+ http://stackoverflow.com/questions/17259280/how-do-you-change-background-for-a-button-mouseover-in-wpf
128
+
129
+  
130
+
131
+  
132
+
133
+  
134
+
135
+ ## ここから下は以前の回答となります
136
+
137
+ ```
138
+
139
+ <Window x:Class="WpfApplication2.MainWindow"
140
+
141
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
142
+
143
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
144
+
145
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
146
+
147
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
148
+
149
+ xmlns:local="clr-namespace:WpfApplication2"
150
+
151
+ mc:Ignorable="d"
152
+
153
+ Title="MainWindow" Height="350" Width="525">
154
+
155
+
156
+
157
+
158
+
159
+ <Window.Resources>
160
+
161
+ <!-- ボタンに表示する画像 -->
162
+
29
163
  <BitmapImage x:Key="ImageSource1" UriSource="./Resources/user.png" />
30
164
 
31
165