teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/18 21:57

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,161 +1,157 @@
1
- `new AdditionalWindow()`や`new MainWindow()`が散見されますが、RyosukeKamimuraさんが意図している動作ではないと思います。
2
-
3
- newするということは別のウィンドウを作るということです。操作しようとしているものとは別のインスタンスです。
4
- `AdditionalWindow`が複数出る必要がない場合、ひとつだけnewし表示非表示で管理すると楽です。
5
-
6
- あと https://teratail.com/questions/233943 で[PropertyGrid](https://github.com/xceedsoftware/wpftoolkit/wiki/PropertyGrid)を使ったのは単にUIを作るのがだるかったからですが、
7
- 自分で言うのもなんですが使いにくくないですか?^^;
8
-
9
- [IntegerUpDown](https://github.com/xceedsoftware/wpftoolkit/wiki/IntegerUpDown)とか[ColorPicker](https://github.com/xceedsoftware/wpftoolkit/wiki/ColorPicker)でUIを作っていただくのがベストですが、自分でもイライラしたのでPropertyGridを少々改良しました。
10
-
11
- 変更したファイルのみ
12
- MainWindow.xaml.cs
13
- ```C#
14
- using System;
15
- using System.Windows;
16
- using System.Windows.Controls;
17
- using System.Windows.Input;
18
- using System.Windows.Media;
19
- using System.Windows.Media.Imaging;
20
- using Microsoft.Win32;
21
-
22
- namespace WpfApp4
23
- {
24
- public partial class MainWindow : Window
25
- {
26
- public string FilePath = null;
27
-
28
- private AdditionalWindow additionalWindow = new AdditionalWindow();
29
-
30
-
31
- public MainWindow() => InitializeComponent();
32
-
33
-
34
- private void button_Click(object sender, RoutedEventArgs e)
35
- {
36
- additionalWindow.Show();
37
- additionalWindow.Owner = this;
38
- }
39
-
40
- private void button_open_Click(object sender, RoutedEventArgs e)
41
- {
42
- OpenFileDialog openFileDialog = new OpenFileDialog();
43
- if(openFileDialog.ShowDialog() == true)
44
- {
45
- FilePath = openFileDialog.FileName;
46
- BitmapImage bitmapImage = new BitmapImage(new Uri(FilePath));
47
- imageBox.Source = bitmapImage;
48
- }
49
- }
50
-
51
- private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
52
- {
53
- if(e.ClickCount == 2)
54
- {
55
- var textBox = new TextBoxEx
56
- {
57
- Style = FindResource("TextBoxStyle") as Style,
58
- CanvasLeft = e.GetPosition(canvas).X,
59
- CanvasTop = e.GetPosition(canvas).Y,
60
- Foreground = new SolidColorBrush(Colors.Red), // 雑い^^;
61
- Background = new SolidColorBrush(),
62
- };
63
- textBox.GotFocus += TextBox_GotFocus;
64
- canvas.Children.Add(textBox);
65
- }
66
- }
67
-
68
- private void DelMenuItem_Click(object sender, RoutedEventArgs e)
69
- {
70
- if(sender is MenuItem menuItem)
71
- {
72
- if(menuItem.Parent is ContextMenu contextMenu)
73
- {
74
- if(contextMenu.PlacementTarget is TextBoxEx textBox)
75
- {
76
- additionalWindow.propertyGrid.SelectedObject = null;
77
- textBox.GotFocus -= TextBox_GotFocus;
78
- canvas.Children.Remove(textBox);
79
- }
80
- }
81
- }
82
- }
83
-
84
- private void TextBox_GotFocus(object sender, RoutedEventArgs e)
85
- {
86
- additionalWindow.propertyGrid.SelectedObject = sender;
87
- }
88
- }
89
- }
90
- ```
91
-
92
- AdditionalWindow.xaml
93
- ```xaml
94
- <Window
95
- x:Class="WpfApp4.AdditionalWindow"
96
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
97
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
98
- xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
99
- Title="AdditionalWindow"
100
- Width="350"
101
- Height="450"
102
- Closing="Window_Closing">
103
- <Grid>
104
- <xctk:PropertyGrid
105
- x:Name="propertyGrid"
106
- AutoGenerateProperties="False"
107
- ShowSearchBox="False"
108
- ShowSortOptions="False"
109
- ShowSummary="False"
110
- ShowTitle="False">
111
- <xctk:PropertyGrid.EditorDefinitions>
112
- <xctk:EditorDefinition>
113
- <xctk:EditorDefinition.PropertiesDefinitions>
114
- <xctk:PropertyDefinition Name="Background" />
115
- <xctk:PropertyDefinition Name="Foreground" />
116
- </xctk:EditorDefinition.PropertiesDefinitions>
117
- <xctk:EditorDefinition.EditorTemplate>
118
- <DataTemplate>
119
- <xctk:ColorPicker SelectedColor="{Binding Value.Color}" />
120
- </DataTemplate>
121
- </xctk:EditorDefinition.EditorTemplate>
122
- </xctk:EditorDefinition>
123
- </xctk:PropertyGrid.EditorDefinitions>
124
-
125
- <xctk:PropertyGrid.PropertyDefinitions>
126
- <xctk:PropertyDefinition Name="CanvasLeft" />
127
- <xctk:PropertyDefinition Name="CanvasTop" />
128
- <xctk:PropertyDefinition Name="Text" />
129
- <xctk:PropertyDefinition Name="Background" />
130
- <xctk:PropertyDefinition Name="FontFamily" />
131
- <xctk:PropertyDefinition Name="FontSize" />
132
- <xctk:PropertyDefinition Name="FontStretch" />
133
- <xctk:PropertyDefinition Name="FontStyle" />
134
- <xctk:PropertyDefinition Name="FontWeight" />
135
- <xctk:PropertyDefinition Name="Foreground" />
136
- </xctk:PropertyGrid.PropertyDefinitions>
137
- </xctk:PropertyGrid>
138
- </Grid>
139
- </Window>
140
- ```
141
-
142
- AdditionalWindow.xaml.cs
143
- ```C#
144
- using System.ComponentModel;
145
- using System.Windows;
146
-
147
- namespace WpfApp4
148
- {
149
- public partial class AdditionalWindow : Window
150
- {
151
- public AdditionalWindow() => InitializeComponent();
152
-
153
- private void Window_Closing(object sender, CancelEventArgs e)
154
- {
155
- // 閉じずに非表示にする
156
- e.Cancel = true;
157
- Hide();
158
- }
159
- }
160
- }
1
+ `new AdditionalWindow()`や`new MainWindow()`が散見されますが、RyosukeKamimuraさんが意図している動作ではないと思います。
2
+
3
+ newするということは別のウィンドウを作るということです。操作しようとしているものとは別のインスタンスです。
4
+ `AdditionalWindow`が複数出る必要がない場合、ひとつだけnewし表示非表示で管理すると楽です。
5
+
6
+ あと https://teratail.com/questions/233943 で[PropertyGrid](https://github.com/xceedsoftware/wpftoolkit/wiki/PropertyGrid)を使ったのは単にUIを作るのがだるかったからですが、自分で言うのもなんですが使いにくくないですか?^^;
7
+
8
+ [IntegerUpDown](https://github.com/xceedsoftware/wpftoolkit/wiki/IntegerUpDown)とか[ColorPicker](https://github.com/xceedsoftware/wpftoolkit/wiki/ColorPicker)でUIを作っていただくのがベストですが、自分でもイライラしたのでPropertyGridを少々改良しました。
9
+
10
+ 変更したファイルのみ
11
+ ```cs:MainWindow.xaml.cs
12
+ using System;
13
+ using System.Windows;
14
+ using System.Windows.Controls;
15
+ using System.Windows.Input;
16
+ using System.Windows.Media;
17
+ using System.Windows.Media.Imaging;
18
+ using Microsoft.Win32;
19
+
20
+ namespace WpfApp4
21
+ {
22
+ public partial class MainWindow : Window
23
+ {
24
+ public string FilePath = null;
25
+
26
+ private AdditionalWindow additionalWindow = new AdditionalWindow();
27
+
28
+
29
+ public MainWindow() => InitializeComponent();
30
+
31
+
32
+ private void button_Click(object sender, RoutedEventArgs e)
33
+ {
34
+ additionalWindow.Show();
35
+ additionalWindow.Owner = this;
36
+ }
37
+
38
+ private void button_open_Click(object sender, RoutedEventArgs e)
39
+ {
40
+ OpenFileDialog openFileDialog = new OpenFileDialog();
41
+ if(openFileDialog.ShowDialog() == true)
42
+ {
43
+ FilePath = openFileDialog.FileName;
44
+ BitmapImage bitmapImage = new BitmapImage(new Uri(FilePath));
45
+ imageBox.Source = bitmapImage;
46
+ }
47
+ }
48
+
49
+ private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
50
+ {
51
+ if(e.ClickCount == 2)
52
+ {
53
+ var textBox = new TextBoxEx
54
+ {
55
+ Style = FindResource("TextBoxStyle") as Style,
56
+ CanvasLeft = e.GetPosition(canvas).X,
57
+ CanvasTop = e.GetPosition(canvas).Y,
58
+ Foreground = new SolidColorBrush(Colors.Red), // 雑い^^;
59
+ Background = new SolidColorBrush(),
60
+ };
61
+ textBox.GotFocus += TextBox_GotFocus;
62
+ canvas.Children.Add(textBox);
63
+ }
64
+ }
65
+
66
+ private void DelMenuItem_Click(object sender, RoutedEventArgs e)
67
+ {
68
+ if(sender is MenuItem menuItem)
69
+ {
70
+ if(menuItem.Parent is ContextMenu contextMenu)
71
+ {
72
+ if(contextMenu.PlacementTarget is TextBoxEx textBox)
73
+ {
74
+ additionalWindow.propertyGrid.SelectedObject = null;
75
+ textBox.GotFocus -= TextBox_GotFocus;
76
+ canvas.Children.Remove(textBox);
77
+ }
78
+ }
79
+ }
80
+ }
81
+
82
+ private void TextBox_GotFocus(object sender, RoutedEventArgs e)
83
+ {
84
+ additionalWindow.propertyGrid.SelectedObject = sender;
85
+ }
86
+ }
87
+ }
88
+ ```
89
+
90
+ ```xml:AdditionalWindow.xaml
91
+ <Window
92
+ x:Class="WpfApp4.AdditionalWindow"
93
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
94
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
95
+ xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
96
+ Title="AdditionalWindow"
97
+ Width="350"
98
+ Height="450"
99
+ Closing="Window_Closing">
100
+ <Grid>
101
+ <xctk:PropertyGrid
102
+ x:Name="propertyGrid"
103
+ AutoGenerateProperties="False"
104
+ ShowSearchBox="False"
105
+ ShowSortOptions="False"
106
+ ShowSummary="False"
107
+ ShowTitle="False">
108
+ <xctk:PropertyGrid.EditorDefinitions>
109
+ <xctk:EditorDefinition>
110
+ <xctk:EditorDefinition.PropertiesDefinitions>
111
+ <xctk:PropertyDefinition Name="Background" />
112
+ <xctk:PropertyDefinition Name="Foreground" />
113
+ </xctk:EditorDefinition.PropertiesDefinitions>
114
+ <xctk:EditorDefinition.EditorTemplate>
115
+ <DataTemplate>
116
+ <xctk:ColorPicker SelectedColor="{Binding Value.Color}" />
117
+ </DataTemplate>
118
+ </xctk:EditorDefinition.EditorTemplate>
119
+ </xctk:EditorDefinition>
120
+ </xctk:PropertyGrid.EditorDefinitions>
121
+
122
+ <xctk:PropertyGrid.PropertyDefinitions>
123
+ <xctk:PropertyDefinition Name="CanvasLeft" />
124
+ <xctk:PropertyDefinition Name="CanvasTop" />
125
+ <xctk:PropertyDefinition Name="Text" />
126
+ <xctk:PropertyDefinition Name="Background" />
127
+ <xctk:PropertyDefinition Name="FontFamily" />
128
+ <xctk:PropertyDefinition Name="FontSize" />
129
+ <xctk:PropertyDefinition Name="FontStretch" />
130
+ <xctk:PropertyDefinition Name="FontStyle" />
131
+ <xctk:PropertyDefinition Name="FontWeight" />
132
+ <xctk:PropertyDefinition Name="Foreground" />
133
+ </xctk:PropertyGrid.PropertyDefinitions>
134
+ </xctk:PropertyGrid>
135
+ </Grid>
136
+ </Window>
137
+ ```
138
+
139
+ ```cs:AdditionalWindow.xaml.cs
140
+ using System.ComponentModel;
141
+ using System.Windows;
142
+
143
+ namespace WpfApp4
144
+ {
145
+ public partial class AdditionalWindow : Window
146
+ {
147
+ public AdditionalWindow() => InitializeComponent();
148
+
149
+ private void Window_Closing(object sender, CancelEventArgs e)
150
+ {
151
+ // 閉じずに非表示にする
152
+ e.Cancel = true;
153
+ Hide();
154
+ }
155
+ }
156
+ }
161
157
  ```