回答編集履歴

1

見直しキャンペーン中

2023/07/21 09:13

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,249 +1,125 @@
1
1
  `Resources`内の`Style`が、`DataTemplate`には当たらないのが原因ですね。
2
-
3
2
  [xaml - WPF Some styles not applied on DataTemplate controls - Stack Overflow](https://stackoverflow.com/questions/2476305/wpf-some-styles-not-applied-on-datatemplate-controls)
4
3
 
4
+ 他の修正点
5
+ * 色を付けた際カラム間の隙間が気になって`<Border x:Name="bg">`を付けたが、気になっていないようなので不要
6
+ * `CheckBox`の`Trigger`が動いていない
5
7
 
6
-
7
- 他の修正点
8
-
9
- * 色を付けた際カラム間の隙間が気になって`<Border x:Name="bg">`を付けたが、気になっていないようなので不要
10
-
11
- * `CheckBox `の`Trigger`が動いていない
12
-
13
-
14
-
15
- ```xaml
8
+ ```xml
16
-
17
9
  <Window
18
-
19
10
  x:Class="Questions250285.MainWindow"
20
-
21
11
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
-
23
12
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
24
-
25
13
  xmlns:local="clr-namespace:Questions250285"
26
-
27
14
  Width="400"
28
-
29
15
  Height="240">
30
-
31
16
  <Window.Resources>
32
-
33
17
  <x:Array x:Key="comment" Type="{x:Type local:User}">
34
-
35
18
  <local:User
36
-
37
19
  Name="WPF C# VS2015"
38
-
39
20
  BkColor="Red"
40
-
41
21
  Data="bbb"
42
-
43
22
  No="1"
44
-
45
23
  SearchResult="True" />
46
-
47
24
  <local:User
48
-
49
25
  Name="WPF C# VS2017"
50
-
51
26
  BkColor="Yellow"
52
-
53
27
  Data="bbbbbbbbb"
54
-
55
28
  No="2" />
56
-
57
29
  <local:User
58
-
59
30
  Name="WPF C# VS2019"
60
-
61
31
  Data="bbbbbbbbbbbbbbbbbbbbbbbbbbb"
62
-
63
32
  No="3" />
64
-
65
33
  </x:Array>
66
-
67
34
  </Window.Resources>
68
-
69
35
  <Grid>
70
-
71
36
  <Grid.RowDefinitions>
72
-
73
37
  <RowDefinition />
74
-
75
38
  <RowDefinition Height="Auto" />
76
-
77
39
  </Grid.RowDefinitions>
78
40
 
79
-
80
-
81
41
  <ListView ItemsSource="{Binding Source={StaticResource comment}}">
82
-
83
42
  <ListView.Resources>
84
-
85
43
  <Style TargetType="{x:Type TextBlock}">
86
-
87
44
  <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}" />
88
-
89
45
  <Style.Triggers>
90
-
91
46
  <DataTrigger Binding="{Binding IsChecked, ElementName=check3}" Value="True">
92
-
93
47
  <Setter Property="TextWrapping" Value="Wrap" />
94
-
95
48
  </DataTrigger>
96
-
97
49
  <DataTrigger Binding="{Binding SearchResult}" Value="True">
98
-
99
50
  <Setter Property="Background" Value="Purple" />
100
-
101
51
  </DataTrigger>
102
-
103
52
  </Style.Triggers>
104
-
105
53
  </Style>
106
54
 
107
-
108
-
109
55
  <Style TargetType="{x:Type ListViewItem}">
110
-
111
56
  <!-- 引き延ばしたほうがいいでしょうか? -->
112
-
113
57
  <Setter Property="HorizontalContentAlignment" Value="Stretch" />
114
-
115
58
  <Setter Property="VerticalContentAlignment" Value="Stretch" />
116
59
 
117
-
118
-
119
60
  <Setter Property="Template">
120
-
121
61
  <Setter.Value>
122
-
123
62
  <ControlTemplate TargetType="{x:Type ListViewItem}">
124
-
125
63
  <GridViewRowPresenter />
126
-
127
64
  </ControlTemplate>
128
-
129
65
  </Setter.Value>
130
-
131
66
  </Setter>
132
-
133
67
  </Style>
134
-
135
68
  </ListView.Resources>
136
69
 
137
-
138
-
139
70
  <!-- 前も指摘しましたがこれはおかしいです -->
140
-
141
71
  <!--<ListView.ItemsPanel>
142
-
143
72
  <ItemsPanelTemplate>
144
-
145
73
  <StackPanel HorizontalAlignment="Center" Orientation="Vertical" />
146
-
147
74
  </ItemsPanelTemplate>
148
-
149
75
  </ListView.ItemsPanel>-->
150
76
 
151
-
152
-
153
77
  <ListView.View>
154
-
155
78
  <GridView>
156
-
157
79
  <GridViewColumn
158
-
159
80
  Width="30"
160
-
161
81
  DisplayMemberBinding="{Binding No}"
162
-
163
82
  Header="No" />
164
-
165
83
  <GridViewColumn
166
-
167
84
  Width="80"
168
-
169
85
  DisplayMemberBinding="{Binding Name}"
170
-
171
86
  Header="Name" />
172
-
173
87
  <GridViewColumn Width="190" Header="Data">
174
-
175
88
  <GridViewColumn.CellTemplate>
176
-
177
89
  <DataTemplate>
178
-
179
90
  <!-- https://stackoverflow.com/questions/2476305/wpf-some-styles-not-applied-on-datatemplate-controls#answer-16974988 -->
180
-
181
91
  <!-- ListView.Resourcesで定義しているスタイルを引っ張ってくる ハックっぽいが2回DataTrigger書くよりいいでしょう -->
182
-
183
92
  <DataTemplate.Resources>
184
-
185
93
  <Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="{x:Type TextBlock}" />
186
-
187
94
  </DataTemplate.Resources>
188
-
189
95
  <!-- Backgroundは上書きするのでPurpleになることはない -->
190
-
191
96
  <TextBlock Background="{Binding BkColor}" Text="{Binding Data}" />
192
-
193
97
  </DataTemplate>
194
-
195
98
  </GridViewColumn.CellTemplate>
196
-
197
99
  </GridViewColumn>
198
-
199
100
  </GridView>
200
-
201
101
  </ListView.View>
202
-
203
102
  </ListView>
204
103
 
205
-
206
-
207
104
  <CheckBox
208
-
209
105
  x:Name="check3"
210
-
211
106
  Grid.Row="1"
212
-
213
107
  VerticalContentAlignment="Center"
214
-
215
108
  Content="テキストを折り返して表示する">
216
-
217
109
  <CheckBox.Style>
218
-
219
110
  <Style TargetType="{x:Type CheckBox}">
220
-
221
111
  <!-- こうでないと意図通りになっていない -->
222
-
223
112
  <Setter Property="Foreground" Value="Red" />
224
-
225
113
  <Style.Triggers>
226
-
227
114
  <Trigger Property="IsChecked" Value="True">
228
-
229
115
  <Setter Property="Foreground" Value="Blue" />
230
-
231
116
  </Trigger>
232
-
233
117
  </Style.Triggers>
234
-
235
118
  </Style>
236
-
237
119
  </CheckBox.Style>
238
-
239
120
  </CheckBox>
240
-
241
121
  </Grid>
242
-
243
122
  </Window>
244
-
245
123
  ```
246
-
247
124
  ![NoWrap](0245197386ed0c7b2fdb7c98c96eaf1b.png)
248
-
249
125
  ![Wrap](bdc82225a2d9ac2a0261eaf0d7282211.png)