回答編集履歴

2

見直しキャンペーン中

2023/07/30 12:52

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,6 +1,6 @@
1
1
  > 「''System.Windows.Media.Animation.ColorAnimation' アニメーション オブジェクトは、互換性のない型 'System.Windows.Media.Brush' であるため、プロパティ 'Background' をアニメーションで表示するためには使用できません。'」というエラーがでます。
2
2
 
3
- エラーの通りListView.Background(Control.Background)は`Brush`型です。
3
+ エラーの通り`ListView.Background``Control.Background`)は`Brush`型です。
4
4
  [Control.Background プロパティ (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.control.background)
5
5
 
6
6
  変えたかったのは`SolidColorBrush`の`Color`でしょう。

1

全コード再掲

2022/08/19 07:49

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -86,3 +86,211 @@
86
86
  </Style>
87
87
  </ListView.ItemContainerStyle>
88
88
  ```
89
+
90
+ ---
91
+
92
+ 全コード再掲
93
+ ```xml
94
+ <Window
95
+ x:Class="Q78aqeaolcdtqmb.MainWindow"
96
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
97
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
98
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
99
+ xmlns:local="clr-namespace:Q78aqeaolcdtqmb"
100
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
101
+ Width="1000"
102
+ Height="450"
103
+ mc:Ignorable="d">
104
+ <Window.Resources>
105
+ <Storyboard x:Key="SearchAnimation">
106
+ <ColorAnimation
107
+ Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
108
+ From="Chocolate"
109
+ To="Cyan"
110
+ Duration="0:0:2" />
111
+ </Storyboard>
112
+ <Storyboard x:Key="StopSearchAnimation">
113
+ <ColorAnimation
114
+ Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
115
+ From="Yellow"
116
+ To="Red"
117
+ Duration="0:0:2" />
118
+ </Storyboard>
119
+ <DataTemplate DataType="{x:Type local:Custmers}">
120
+ <StackPanel Margin="2">
121
+ <TextBox
122
+ Width="120"
123
+ HorizontalAlignment="Center"
124
+ FontSize="15"
125
+ Text="{Binding [0].Dr}" />
126
+ <ListView ItemsSource="{Binding}">
127
+ <ListView.Resources>
128
+ <Style TargetType="GridViewColumnHeader">
129
+ <Setter Property="Template" Value="{x:Null}" />
130
+ </Style>
131
+ </ListView.Resources>
132
+ <ListView.ItemContainerStyle>
133
+ <Style TargetType="ListViewItem">
134
+ <Style.Triggers>
135
+ <DataTrigger Binding="{Binding Check}" Value="True">
136
+ <DataTrigger.EnterActions>
137
+ <BeginStoryboard Storyboard="{StaticResource SearchAnimation}" />
138
+ </DataTrigger.EnterActions>
139
+ <DataTrigger.ExitActions>
140
+ <BeginStoryboard Storyboard="{StaticResource StopSearchAnimation}" />
141
+ </DataTrigger.ExitActions>
142
+ </DataTrigger>
143
+ </Style.Triggers>
144
+ </Style>
145
+ </ListView.ItemContainerStyle>
146
+ <ListView.View>
147
+ <GridView>
148
+ <GridViewColumn DisplayMemberBinding="{Binding No}" />
149
+ <GridViewColumn DisplayMemberBinding="{Binding KanjaMei}" />
150
+ <GridViewColumn>
151
+ <GridViewColumn.CellTemplate>
152
+ <DataTemplate>
153
+ <CheckBox IsChecked="{Binding ScCheck}" />
154
+ </DataTemplate>
155
+ </GridViewColumn.CellTemplate>
156
+ </GridViewColumn>
157
+ </GridView>
158
+ </ListView.View>
159
+ </ListView>
160
+ </StackPanel>
161
+ </DataTemplate>
162
+ </Window.Resources>
163
+
164
+ <DockPanel>
165
+ <Button
166
+ Click="AddButton_Click"
167
+ Content="add"
168
+ DockPanel.Dock="Top" />
169
+ <Button
170
+ Click="HighlightButton_Click"
171
+ Content="highlight"
172
+ DockPanel.Dock="Top" />
173
+ <Grid>
174
+ <Grid.ColumnDefinitions>
175
+ <ColumnDefinition />
176
+ <ColumnDefinition />
177
+ </Grid.ColumnDefinitions>
178
+ <!-- 元データ -->
179
+ <DataGrid
180
+ x:Name="dataGrid"
181
+ AutoGenerateColumns="True"
182
+ ItemsSource="{Binding Model.Custmers}" />
183
+
184
+ <ScrollViewer
185
+ Grid.Column="1"
186
+ Margin="5"
187
+ HorizontalScrollBarVisibility="Auto">
188
+ <ItemsControl ItemsSource="{Binding CustmersList}">
189
+ <ItemsControl.ItemsPanel>
190
+ <ItemsPanelTemplate>
191
+ <StackPanel Orientation="Horizontal" />
192
+ </ItemsPanelTemplate>
193
+ </ItemsControl.ItemsPanel>
194
+ </ItemsControl>
195
+ </ScrollViewer>
196
+ </Grid>
197
+ </DockPanel>
198
+ </Window>
199
+ ```
200
+ ```cs
201
+ using System;
202
+ using System.Collections.Generic;
203
+ using System.Collections.ObjectModel;
204
+ using System.Linq;
205
+ using System.Windows;
206
+ using CommunityToolkit.Mvvm.ComponentModel;
207
+
208
+ namespace Q78aqeaolcdtqmb;
209
+
210
+
211
+ public class Model
212
+ {
213
+ public Custmers Custmers { get; } = new();
214
+ }
215
+
216
+ public class Custmer : ObservableObject
217
+ {
218
+ public int No { get; }
219
+ public string Hyohon { get; }
220
+ public string KanjaMei { get; }
221
+ public string Dr { get; set; }
222
+ public bool ScCheck { get; set; }
223
+
224
+ public bool Check { get => check; set => SetProperty(ref check, value); }
225
+ private bool check;
226
+
227
+ public Custmer(int no, string hyohon, string kanjaMei, string dr, bool scCheck)
228
+ {
229
+ (No, Hyohon, KanjaMei, Dr, ScCheck) = (no, hyohon, kanjaMei, dr, scCheck);
230
+ }
231
+
232
+
233
+ // ダミー作成用
234
+ private static int no = 1300;
235
+ private static Random r = new();
236
+ private static string k = "アイウエオカキクケコサシスセソタチツテトナニヌネノナヒフヘホマミムメモヤユヨラリルレロワ";
237
+ public Custmer()
238
+ {
239
+ No = ++no;
240
+ Hyohon = $"F222{No}";
241
+ KanjaMei = string.Join("", Enumerable.Range(0, r.Next(5, 10)).Select(x => k[r.Next(k.Length)]));
242
+ Dr = $"dr{No}";
243
+ ScCheck = false;
244
+ }
245
+ }
246
+
247
+ public class Custmers : List<Custmer> { } // xamlでジェネリック指定できないので型を作る
248
+
249
+
250
+ public partial class MainWindow : Window
251
+ {
252
+ public Model Model { get; } = new();
253
+
254
+ public ObservableCollection<Custmers> CustmersList { get; } = new();
255
+
256
+ public MainWindow()
257
+ {
258
+ InitializeComponent();
259
+
260
+ foreach (var _ in Enumerable.Range(1, 100))
261
+ {
262
+ Model.Custmers.Add(new Custmer());
263
+ }
264
+ var custmers = new Custmers();
265
+ custmers.AddRange(Model.Custmers.Take(5));
266
+ CustmersList.Add(custmers);
267
+
268
+ DataContext = this;
269
+ }
270
+
271
+ private void AddButton_Click(object sender, RoutedEventArgs e)
272
+ {
273
+ var custmers = new Custmers();
274
+ // DataGridで選択中を抽出
275
+ custmers.AddRange(dataGrid.SelectedItems.Cast<Custmer>());
276
+ // ItemsControlに追加
277
+ CustmersList.Add(custmers);
278
+ }
279
+
280
+ private void HighlightButton_Click(object sender, RoutedEventArgs e)
281
+ {
282
+ // ハイライトを全クリア
283
+ foreach (var custmer in Model.Custmers)
284
+ {
285
+ custmer.Check = false;
286
+ }
287
+
288
+ // DataGridで選択中をハイライト
289
+ foreach (Custmer custmer in dataGrid.SelectedItems)
290
+ {
291
+ custmer.Check = true;
292
+ }
293
+ }
294
+ }
295
+ ```
296
+ ![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2022-08-19/d1351f80-9be8-46f2-8a79-c4e119f291b2.png)