質問編集履歴

1

ソースコードの修正

2022/04/29 02:04

投稿

tranokado
tranokado

スコア4

test CHANGED
File without changes
test CHANGED
@@ -17,158 +17,285 @@
17
17
 
18
18
  ### 該当のソースコード
19
19
  ```xaml
20
- <Window x:Class="ManagerEditor.MainWindow"
21
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
23
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
24
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
25
- xmlns:local="clr-namespace:ManagerEditor"
26
- mc:Ignorable="d"
27
- Title="MainWindow" Height="800" Width="1100">
28
- <Grid>
29
- <Grid x:Name="LeftRoot" Margin="10,10,-10,-10">
30
- <Grid.ColumnDefinitions>
31
- <ColumnDefinition Width="10*"/>
32
- <ColumnDefinition Width="27*"/>
33
- </Grid.ColumnDefinitions>
34
- <Button x:Name="OpenButton" Width="120" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Content="開く" FontSize="18" Click="OpenButton_Click" />
35
- <TextBlock HorizontalAlignment="Left" Margin="10,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" Width="101" FontSize="18"><Run Language="ja-jp" Text="name"/><LineBreak/><Run Language="ja-jp"/></TextBlock>
36
- <ListBox x:Name="UnitnameListBox" SelectionChanged="UnitNameListBox_SelectionChanged" Margin="0,100,0,12" ScrollViewer.HorizontalScrollBarVisibility="Disabled" DisplayMemberPath="name" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" HorizontalAlignment="Center" Width="275" />
37
-
38
- <TextBlock x:Name="Display" Grid.Column="2" HorizontalAlignment="Left" Margin="10,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18"><Run Language="ja-jp" Text="name"/><LineBreak/><Run Language="ja-jp"/></TextBlock>
39
- <TextBlock x:Name="DisplayBox" Grid.Column="1" HorizontalAlignment="Left" Margin="10,78,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="326" Height="46"/>
40
-
41
- <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="10,122,0,0" TextWrapping="Wrap" Text="URL" VerticalAlignment="Top" FontSize="18"/>
42
- <TextBlock x:Name="URLBox" Grid.Column="1" HorizontalAlignment="Left" Margin="10,151,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="39" Width="595"/>
43
-
44
- <TextBlock HorizontalAlignment="Left" Margin="10,194,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" Width="116" FontSize="18" Grid.Column="1"><Run Language="ja-jp" Text="subname"/></TextBlock>
45
- <ListBox x:Name="SubnameListBox" Margin="10,230,27,12" ScrollViewer.HorizontalScrollBarVisibility="Disabled" DisplayMemberPath="subname" ItemsSource="{Binding / manager_list}" Grid.Column="1" />
46
-
47
- </Grid>
48
-
20
+ <Window
21
+ x:Class="Qogxgft0o3rrwcj.MainWindow"
22
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
23
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
24
+ xmlns:dd="urn:gong-wpf-dragdrop"
25
+ xmlns:local="clr-namespace:Qogxgft0o3rrwcj"
26
+ Width="800"
27
+ Height="450">
28
+ <Window.Resources>
29
+ <local:ManagerDropHandler x:Key="ManagerDropHandler" />
30
+ <Style TargetType="Button">
31
+ <Setter Property="Margin" Value="10,5" />
32
+ <Setter Property="MinWidth" Value="80" />
33
+ </Style>
34
+ <Style TargetType="GroupBox">
35
+ <Setter Property="BorderThickness" Value="0" />
36
+ </Style>
37
+ </Window.Resources>
38
+
39
+ <Grid Margin="10">
40
+ <Grid.ColumnDefinitions>
41
+ <ColumnDefinition Width="10*" />
42
+ <ColumnDefinition Width="27*" />
43
+ </Grid.ColumnDefinitions>
44
+
45
+ <DockPanel>
46
+ <Button
47
+ Click="OpenButton_Click"
48
+ Content="開く"
49
+ DockPanel.Dock="Top" />
50
+ <Button
51
+ Click="SaveAsButton_Click"
52
+ Content="保存"
53
+ DockPanel.Dock="Top" />
54
+
55
+ <GroupBox Header="name">
56
+ <ListBox
57
+ x:Name="nameListBox"
58
+ dd:DragDrop.DropHandler="{StaticResource ManagerDropHandler}"
59
+ dd:DragDrop.IsDragSource="True"
60
+ dd:DragDrop.IsDropTarget="True"
61
+ DisplayMemberPath="name"
62
+ IsSynchronizedWithCurrentItem="True"
63
+ ItemsSource="{Binding}" />
64
+ </GroupBox>
65
+ </DockPanel>
66
+
67
+ <Grid Grid.Column="1">
68
+ <Grid.RowDefinitions>
69
+ <RowDefinition Height="Auto" />
70
+ <RowDefinition />
71
+ <RowDefinition Height="Auto" />
72
+ </Grid.RowDefinitions>
73
+
74
+ <DockPanel>
75
+ <StackPanel DockPanel.Dock="Right">
76
+ <Button Click="Name_NewButton_Click" Content="新規" />
77
+ <Button Click="Name_DelButton_Click" Content="削除" />
78
+ </StackPanel>
79
+ <StackPanel>
80
+ <GroupBox DockPanel.Dock="Top" Header="name">
81
+ <TextBox Text="{Binding /name, UpdateSourceTrigger=PropertyChanged}" />
82
+ </GroupBox>
83
+ <GroupBox DockPanel.Dock="Top" Header="URL">
84
+ <TextBox Text="{Binding /url, UpdateSourceTrigger=PropertyChanged}" />
85
+ </GroupBox>
86
+ </StackPanel>
87
+ </DockPanel>
88
+
89
+ <GroupBox Grid.Row="1" Header="subname">
90
+ <ListBox
91
+ x:Name="subNameListBox"
92
+ dd:DragDrop.IsDragSource="True"
93
+ dd:DragDrop.IsDropTarget="True"
94
+ DisplayMemberPath="subname"
95
+ IsSynchronizedWithCurrentItem="True"
96
+ ItemsSource="{Binding /manager_list}" />
97
+ </GroupBox>
98
+
99
+ <DockPanel Grid.Row="2">
100
+ <StackPanel DockPanel.Dock="Right">
101
+ <Button Click="SubName_NewButton_Click" Content="新規" />
102
+ <Button Click="SubName_DelButton_Click" Content="削除" />
103
+ </StackPanel>
104
+
105
+ <StackPanel>
106
+ <GroupBox Header="subname">
107
+ <TextBox Text="{Binding /manager_list/subname, UpdateSourceTrigger=PropertyChanged}" />
108
+ </GroupBox>
109
+ <GroupBox Header="address">
110
+ <TextBox Text="{Binding /manager_list/address, UpdateSourceTrigger=PropertyChanged}" />
111
+ </GroupBox>
112
+ </StackPanel>
113
+ </DockPanel>
49
114
  </Grid>
115
+ </Grid>
50
116
  </Window>
51
117
  ```
52
118
  ```C#
119
+ using System.Collections.ObjectModel;
120
+ using System.IO;
121
+ using System.Windows;
122
+ using GongSolutions.Wpf.DragDrop;
53
- // Copyright © 2022 Tsubasa co., Ltd. All rights reserved.
123
+ using GongSolutions.Wpf.DragDrop.Utilities;
54
124
  using Microsoft.Win32;
55
125
  using Newtonsoft.Json;
56
- using System;
57
- using System.Collections.Generic;
58
- using System.Diagnostics;
59
- using System.IO;
60
- using System.Windows;
61
- using System.Windows.Controls;
126
+ using System.Windows.Forms;
62
-
127
+
63
- namespace ManagerEditor
128
+ namespace Qogxgft0o3rrwcj
64
129
  {
65
- /// <summary>
66
- /// メイン画面
67
- /// </summary>
68
- public partial class MainWindow : Window
69
- {
70
- public MainWindow()
71
- {
72
- InitializeComponent();
73
- }
74
-
75
- private void Window_Loaded(object sender, RoutedEventArgs e)
76
- {
77
-
78
- }
79
-
80
- PersonalData[] _data;
81
-
82
-
83
- //Jsonデータのデシリアライズ、格納
84
- private void OpenButton_Click(object sender, RoutedEventArgs e)
85
- {
86
- var dialog = new OpenFileDialog();
87
- dialog.Filter = "json|*.json";
88
- if (dialog.ShowDialog().Value)
89
- {
90
- var file = dialog.FileName;
91
-
92
- try
93
- {
94
- var reader = new StreamReader(file);
95
- var data = reader.ReadToEnd();
96
- reader.Close();
97
- _data = JsonConvert.DeserializeObject<PersonalData[]>(data);
98
-
99
- }
100
- catch (Exception error)
101
- {
102
- if (error.Message != null)
103
- Debug.WriteLine(error.Message);
104
- }
105
-
106
- if (_data != null)
107
- {
108
- DataContext = _data;
109
- }
110
-
111
- URLBox.Text = "";
112
- DisplayBox.Text = "";
113
- }
114
- }
115
-
116
- //グループ押下時の挙動設定
117
- private void UnitNameListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
118
- {
119
- if (UnitnameListBox.SelectedItem == null) return;
120
-
121
- PersonalData item = (PersonalData)UnitnameListBox.SelectedItem;
122
- DisplayBox.Text = item.name;
123
- URLBox.Text = item.url;
124
- }
125
-
126
- }
127
- public class Manager
128
- {
129
- public string subname { get; set; }
130
- public string address { get; set; }
131
- }
132
-
133
- public class PersonalData
134
- {
135
- public string name { get; set; }
136
- public string url { get; set; }
137
- public List<Manager> manager_list { get; set; }
138
- }
139
-
140
- }
141
- ```
142
- ```json
130
+ public class Manager
131
+ {
132
+ public string subname { get; set; }
133
+ public string address { get; set; }
134
+ }
135
+
136
+ public class PersonalData
137
+ {
138
+ public string name { get; set; }
139
+ public string url { get; set; }
140
+ public ObservableCollection<Manager> manager_list { get; set; }
141
+ }
142
+
143
+ public partial class MainWindow : Window
144
+ {
145
+ private ObservableCollection<PersonalData> _data = new ObservableCollection<PersonalData>();
146
+
147
+ public MainWindow()
148
+ {
149
+ InitializeComponent();
150
+ DataContext = _data;
151
+
152
+ Dummy();
153
+ }
154
+
155
+ private void OpenButton_Click(object sender, RoutedEventArgs e)
156
+ {
157
+ var dialog = new OpenFileDialog { Filter = "json|*.json", };
158
+ if (dialog.ShowDialog() == true)
159
+ {
160
+ var json = File.ReadAllText(dialog.FileName);
161
+ _data = JsonConvert.DeserializeObject<ObservableCollection<PersonalData>>(json);
162
+ DataContext = _data;
163
+ }
164
+ }
165
+ private void SaveAsButton_Click(object sender, RoutedEventArgs e)
166
+ {
167
+ var dialog = new SaveFileDialog { Filter = "json|*.json", };
168
+ if (dialog.ShowDialog() == true)
169
+ {
170
+ var json = JsonConvert.SerializeObject(_data, Formatting.Indented);
171
+ File.WriteAllText(dialog.FileName, json);
172
+ }
173
+ }
174
+
175
+ private void Name_NewButton_Click(object sender, RoutedEventArgs e)
176
+ {
177
+ var data = new PersonalData
178
+ {
179
+ name = "new name",
180
+ manager_list = new ObservableCollection<Manager>(),
181
+ };
182
+ _data.Add(data);
183
+ nameListBox.SelectedItem = data;
184
+ }
185
+ private void Name_DelButton_Click(object sender, RoutedEventArgs e)
186
+ {
187
+ DialogResult result = MessageBox.Show("msg", "タイトル", MessageBoxButtons.YesNo);
188
+ if (nameListBox.SelectedItem is PersonalData data)
189
+ {
190
+ _data.Remove(data);
191
+ }
192
+ }
193
+
194
+ private void SubName_NewButton_Click(object sender, RoutedEventArgs e)
195
+ {
196
+ if (nameListBox.SelectedItem is PersonalData data)
197
+ {
198
+ var manager = new Manager { subname = "new subname", };
199
+ data.manager_list.Add(manager);
200
+ subNameListBox.SelectedItem = manager;
201
+ }
202
+ }
203
+ private void SubName_DelButton_Click(object sender, RoutedEventArgs e)
204
+ {
205
+ if (nameListBox.SelectedItem is PersonalData data
206
+ && subNameListBox.SelectedItem is Manager manager)
207
+ {
208
+ data.manager_list.Remove(manager);
209
+ }
210
+ }
211
+
212
+
213
+ private void Dummy()
214
+ {
215
+ if (File.Exists("test.json")) return;
216
+
217
+ var json = @"
143
218
  [
144
219
  {
145
- "name": "tora",
220
+ ""name"": ""tora"",
146
- "url": "https:tora.com",
221
+ ""url"": ""https:tora.com"",
147
- "manager_list": [
222
+ ""manager_list"": [
148
223
  {
149
- "subname": "tiger",
224
+ ""subname"": ""tiger"",
150
- "address": "chiba"
225
+ ""address"": ""chiba""
151
226
  },
152
227
  {
153
- "subname": "tiger2",
228
+ ""subname"": ""tiger2"",
154
- "address": "tokyo"
229
+ ""address"": ""tokyo""
155
230
  }
156
-
157
231
  ]
158
232
  },
159
233
  {
160
- "name": "cat",
234
+ ""name"": ""cat"",
161
- "url": "https:cat.com",
235
+ ""url"": ""https:cat.com"",
162
- "manager_list": [
236
+ ""manager_list"": [
163
237
  {
164
- "subname": "neko",
238
+ ""subname"": ""neko"",
165
- "address": "kanagawa"
239
+ ""address"": ""kanagawa""
166
240
  },
167
241
  {
168
- "subname": "neko2",
242
+ ""subname"": ""neko2"",
169
- "address": "yokohama"
243
+ ""address"": ""yokohama""
170
244
  }
171
245
  ]
172
246
  }
173
247
  ]
248
+ ".Trim();
249
+
250
+ File.WriteAllText("test.json", json);
251
+ }
252
+ }
253
+
254
+
255
+ class ManagerDropHandler : DefaultDropHandler
256
+ {
257
+ public override void DragOver(IDropInfo dropInfo)
258
+ {
259
+ // nameListBox内アイテム並び替え(デフォルト動作)
260
+ if (dropInfo.Data is PersonalData && dropInfo.TargetItem is PersonalData)
261
+ {
262
+ base.DragOver(dropInfo);
263
+ }
264
+ // subNameListBox→nameListBox アイテム移動・コピー(Ctrl+ドラッグ)
265
+ else if (dropInfo.Data is Manager && dropInfo.TargetItem is PersonalData)
266
+ {
267
+ var isCopy = dropInfo.KeyStates.HasFlag(DragDropKeyStates.ControlKey);
268
+ dropInfo.Effects = isCopy ? DragDropEffects.Copy : DragDropEffects.Move;
269
+ dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
270
+ }
271
+ }
272
+
273
+ public override void Drop(IDropInfo dropInfo)
274
+ {
275
+ if (dropInfo?.DragInfo == null) return;
276
+
277
+ // nameListBox内アイテム並び替え(デフォルト動作)
278
+ if (dropInfo.Data is PersonalData && dropInfo.TargetItem is PersonalData)
279
+ {
280
+ base.Drop(dropInfo);
281
+ }
282
+ // subNameListBox→nameListBox アイテム移動・コピー(Ctrl+ドラッグ)
283
+ else if (dropInfo.Data is Manager source && dropInfo.TargetItem is PersonalData target)
284
+ {
285
+ var sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();
286
+ if (dropInfo.Effects == DragDropEffects.Copy)
287
+ {
288
+ var copy = new Manager { subname = source.subname, address = source.address, };
289
+ target.manager_list.Add(copy);
290
+ }
291
+ else if (dropInfo.Effects == DragDropEffects.Move)
292
+ {
293
+ sourceList.Remove(source);
294
+ target.manager_list.Add(source);
295
+ }
296
+ }
297
+ }
298
+ }
299
+ }
174
300
  ```
301
+