回答編集履歴

1

見直しキャンペーン中

2023/07/28 16:37

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,191 +1,96 @@
1
1
  参考記事には肝心の`Photos`の定義がないですね。
2
-
3
2
  書き忘れたのか、あるいは自明だろうということで省略したのかもしれません。
4
3
 
5
-
6
-
7
4
  普通はViewModelに定義しますが、回答では`MainWindow`に書きました。
8
-
9
5
  `ObservableCollection`は、追加削除を通知するコレクションです(詳しくはググってください)
10
6
 
11
-
12
-
13
- ```xaml
7
+ ```xml
14
-
15
8
  <Window
16
-
17
9
  x:Class="Questions354973.MainWindow"
18
-
19
10
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
20
-
21
11
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
22
-
23
12
  Width="800"
24
-
25
13
  Height="450">
26
-
27
14
  <Grid>
28
-
29
15
  <ListView
30
-
31
16
  ItemsSource="{Binding Photos}"
32
-
33
17
  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
34
-
35
18
  SelectionMode="Single">
36
-
37
19
  <ListView.ItemsPanel>
38
-
39
20
  <ItemsPanelTemplate>
40
-
41
21
  <WrapPanel />
42
-
43
22
  </ItemsPanelTemplate>
44
-
45
23
  </ListView.ItemsPanel>
46
-
47
24
  <ListView.ItemTemplate>
48
-
49
25
  <DataTemplate>
50
-
51
26
  <Grid Width="300" Height="200">
52
-
53
27
  <Image Source="{Binding Title}" />
54
-
55
28
  <TextBlock Text="{Binding Url}" />
56
-
57
29
  </Grid>
58
-
59
30
  </DataTemplate>
60
-
61
31
  </ListView.ItemTemplate>
62
-
63
32
  </ListView>
64
-
65
33
  <Button
66
-
67
34
  MinWidth="75"
68
-
69
35
  Margin="10"
70
-
71
36
  HorizontalAlignment="Left"
72
-
73
37
  VerticalAlignment="Bottom"
74
-
75
38
  Click="Button_Click"
76
-
77
39
  Content="Button" />
78
-
79
40
  </Grid>
80
-
81
41
  </Window>
82
-
83
42
  ```
84
43
 
85
-
86
-
87
- ```C#
44
+ ```cs
88
-
89
45
  using Microsoft.WindowsAPICodePack.Dialogs;
90
-
91
46
  using System.Collections.ObjectModel;
92
-
93
47
  using System.IO;
94
-
95
48
  using System.Windows;
96
49
 
97
-
98
-
99
50
  namespace Questions354973
100
-
101
51
  {
102
-
103
52
  public class Photo
104
-
105
53
  {
106
-
107
54
  public string Title { get; set; }
108
-
109
55
  public string Url { get; set; }
110
-
111
56
  }
112
57
 
113
-
114
-
115
58
  public partial class MainWindow : Window
116
-
117
59
  {
118
-
119
60
  public ObservableCollection<Photo> Photos { get; } = new ObservableCollection<Photo>();
120
61
 
121
-
122
-
123
62
  public MainWindow()
124
-
125
63
  {
126
-
127
64
  InitializeComponent();
128
-
129
65
  DataContext = this;
130
-
131
66
  }
132
67
 
133
-
134
-
135
68
  private void Button_Click(object sender, RoutedEventArgs e)
136
-
137
69
  {
138
-
139
70
  using (var cofd = new CommonOpenFileDialog()
140
-
141
71
  {
142
-
143
72
  Title = "フォルダを選択してください",
144
-
145
73
  InitialDirectory = @"C:\",
146
-
147
74
  IsFolderPicker = true,
148
-
149
75
  })
150
-
151
76
  {
152
-
153
77
  if (cofd.ShowDialog() != CommonFileDialogResult.Ok) return;
154
-
155
-
156
78
 
157
79
  MessageBox.Show($"{cofd.FileName}を選択しました");
158
80
 
159
-
160
-
161
81
  var files = Directory.GetFiles(cofd.FileName, "*.png", SearchOption.AllDirectories);
162
-
163
82
  foreach (var file in files)
164
-
165
83
  {
166
-
167
84
  var photo = new Photo
168
-
169
85
  {
170
-
171
86
  Title = file,
172
-
173
87
  Url = file,
174
-
175
88
  };
176
89
 
177
-
178
-
179
90
  Photos.Add(photo);
180
-
181
91
  }
182
-
183
92
  }
184
-
185
93
  }
186
-
187
94
  }
188
-
189
95
  }
190
-
191
96
  ```