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

回答編集履歴

2

見直しキャンペーン中

2023/08/15 09:45

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,12 +1,13 @@
1
1
  `MenuItem.ToolTip`プロパティは`object`のため、`ToolTip`クラスを入れた場合はキャストがいります。
2
2
  [FrameworkElement.ToolTip プロパティ (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.frameworkelement.tooltip?view=net-6.0)
3
3
 
4
- shindan[j].Dataがなんだかわかりませんが、今`Anisakiasis`と出てるんだったら、
4
+ `shindan[j].Data`がなんだかわかりませんが、今`Anisakiasis`と出てるんだったら、↓でいいはずです。
5
5
  ```cs
6
6
  textEditor2.Text += ((ToolTip)item.ToolTip).Content.ToString();
7
7
  ```
8
- でいいはずです。
9
8
 
9
+ ---
10
+
10
11
  ```xml
11
12
  <Window
12
13
  x:Class="Questions375250.MainWindow"

1

見直しキャンペーン中

2023/07/29 13:38

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,108 +1,108 @@
1
- `MenuItem.ToolTip`プロパティは`object`のため、`ToolTip`クラスを入れた場合はキャストがいります。
2
- [FrameworkElement.ToolTip プロパティ (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.frameworkelement.tooltip?view=net-6.0)
3
-
4
- shindan[j].Dataがなんだかわかりませんが、今`Anisakiasis`と出てるんだったら、
5
- ```C#
6
- textEditor2.Text += ((ToolTip)item.ToolTip).Content.ToString();
7
- ```
8
- でいいはずです。
9
-
10
- ```xaml
11
- <Window
12
- x:Class="Questions375250.MainWindow"
13
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
- Width="800"
16
- Height="450">
17
- <Window.Resources>
18
- <Style TargetType="ToolTip">
19
- <Style.Resources>
20
- <Style TargetType="ContentPresenter">
21
- <Style.Resources>
22
- <Style TargetType="TextBlock">
23
- <Setter Property="TextWrapping" Value="Wrap" />
24
- </Style>
25
- </Style.Resources>
26
- </Style>
27
- </Style.Resources>
28
- <Setter Property="MaxWidth" Value="600" />
29
- <Setter Property="Background" Value="Bisque" />
30
- </Style>
31
- </Window.Resources>
32
-
33
- <DockPanel>
34
- <Menu x:Name="menu" DockPanel.Dock="Top">
35
- <MenuItem
36
- Click="FileMenuItem_Click"
37
- Header="ファイル(_F)"
38
- ToolTip="ファイル - つーるちっぷ" />
39
- </Menu>
40
- <TextBox x:Name="textBox" />
41
- </DockPanel>
42
- </Window>
43
- ```
44
-
45
- ```C#
46
- using System.Windows;
47
- using System.Windows.Controls;
48
- using System.Windows.Media;
49
-
50
- namespace Questions375250
51
- {
52
- public partial class MainWindow : Window
53
- {
54
- public MainWindow()
55
- {
56
- InitializeComponent();
57
-
58
- var editMenuItem = new MenuItem
59
- {
60
- Background = Brushes.Beige,
61
- Header = "編集(_E)",
62
- ToolTip = new ToolTip { Content = "編集 - つーるちっぷ", },
63
- };
64
- editMenuItem.Click += EditMenuItem_Click;
65
- menu.Items.Add(editMenuItem);
66
-
67
- var viewMenuItem = new MenuItem
68
- {
69
- Background = Brushes.Beige,
70
- Header = "表示(_V)",
71
- ToolTip = new ToolTip { Content = "表示 - つーるちっぷ", },
72
- };
73
- viewMenuItem.Click += ViewMenuItem_Click;
74
- menu.Items.Add(viewMenuItem);
75
- }
76
-
77
- private void FileMenuItem_Click(object sender, RoutedEventArgs e)
78
- {
79
- var item = (MenuItem)sender;
80
-
81
- // ファイルMenuItemはToolTipプロパティに直接文字列だから、item.ToolTipは文字列
82
- textBox.Text += item.ToolTip.ToString(); // ファイル - つーるちっぷ
83
- textBox.Text += "\n";
84
- }
85
-
86
- private void EditMenuItem_Click(object sender, RoutedEventArgs e)
87
- {
88
- var item = (MenuItem)sender;
89
-
90
- // 編集MenuItemはToolTipプロパティにToolTipクラスが入っているから、item.ToolTipはToolTipクラス
91
- // ToStringすると型名が出る
92
- textBox.Text += item.ToolTip.ToString(); // System.Windows.Controls.ToolTip: 編集 - つーるちっぷ
93
- textBox.Text += "\n";
94
- }
95
-
96
- private void ViewMenuItem_Click(object sender, RoutedEventArgs e)
97
- {
98
- var item = (MenuItem)sender;
99
-
100
- // 表示MenuItemはToolTipプロパティにToolTipクラスが入っているから、item.ToolTipはToolTipクラス
101
- // キャストしてContentで取り出せる
102
- textBox.Text += ((ToolTip)item.ToolTip).Content.ToString(); // 表示 - つーるちっぷ
103
- textBox.Text += "\n";
104
- }
105
- }
106
- }
107
- ```
1
+ `MenuItem.ToolTip`プロパティは`object`のため、`ToolTip`クラスを入れた場合はキャストがいります。
2
+ [FrameworkElement.ToolTip プロパティ (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.frameworkelement.tooltip?view=net-6.0)
3
+
4
+ shindan[j].Dataがなんだかわかりませんが、今`Anisakiasis`と出てるんだったら、
5
+ ```cs
6
+ textEditor2.Text += ((ToolTip)item.ToolTip).Content.ToString();
7
+ ```
8
+ でいいはずです。
9
+
10
+ ```xml
11
+ <Window
12
+ x:Class="Questions375250.MainWindow"
13
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
+ Width="800"
16
+ Height="450">
17
+ <Window.Resources>
18
+ <Style TargetType="ToolTip">
19
+ <Style.Resources>
20
+ <Style TargetType="ContentPresenter">
21
+ <Style.Resources>
22
+ <Style TargetType="TextBlock">
23
+ <Setter Property="TextWrapping" Value="Wrap" />
24
+ </Style>
25
+ </Style.Resources>
26
+ </Style>
27
+ </Style.Resources>
28
+ <Setter Property="MaxWidth" Value="600" />
29
+ <Setter Property="Background" Value="Bisque" />
30
+ </Style>
31
+ </Window.Resources>
32
+
33
+ <DockPanel>
34
+ <Menu x:Name="menu" DockPanel.Dock="Top">
35
+ <MenuItem
36
+ Click="FileMenuItem_Click"
37
+ Header="ファイル(_F)"
38
+ ToolTip="ファイル - つーるちっぷ" />
39
+ </Menu>
40
+ <TextBox x:Name="textBox" />
41
+ </DockPanel>
42
+ </Window>
43
+ ```
44
+
45
+ ```cs
46
+ using System.Windows;
47
+ using System.Windows.Controls;
48
+ using System.Windows.Media;
49
+
50
+ namespace Questions375250
51
+ {
52
+ public partial class MainWindow : Window
53
+ {
54
+ public MainWindow()
55
+ {
56
+ InitializeComponent();
57
+
58
+ var editMenuItem = new MenuItem
59
+ {
60
+ Background = Brushes.Beige,
61
+ Header = "編集(_E)",
62
+ ToolTip = new ToolTip { Content = "編集 - つーるちっぷ", },
63
+ };
64
+ editMenuItem.Click += EditMenuItem_Click;
65
+ menu.Items.Add(editMenuItem);
66
+
67
+ var viewMenuItem = new MenuItem
68
+ {
69
+ Background = Brushes.Beige,
70
+ Header = "表示(_V)",
71
+ ToolTip = new ToolTip { Content = "表示 - つーるちっぷ", },
72
+ };
73
+ viewMenuItem.Click += ViewMenuItem_Click;
74
+ menu.Items.Add(viewMenuItem);
75
+ }
76
+
77
+ private void FileMenuItem_Click(object sender, RoutedEventArgs e)
78
+ {
79
+ var item = (MenuItem)sender;
80
+
81
+ // ファイルMenuItemはToolTipプロパティに直接文字列だから、item.ToolTipは文字列
82
+ textBox.Text += item.ToolTip.ToString(); // ファイル - つーるちっぷ
83
+ textBox.Text += "\n";
84
+ }
85
+
86
+ private void EditMenuItem_Click(object sender, RoutedEventArgs e)
87
+ {
88
+ var item = (MenuItem)sender;
89
+
90
+ // 編集MenuItemはToolTipプロパティにToolTipクラスが入っているから、item.ToolTipはToolTipクラス
91
+ // ToStringすると型名が出る
92
+ textBox.Text += item.ToolTip.ToString(); // System.Windows.Controls.ToolTip: 編集 - つーるちっぷ
93
+ textBox.Text += "\n";
94
+ }
95
+
96
+ private void ViewMenuItem_Click(object sender, RoutedEventArgs e)
97
+ {
98
+ var item = (MenuItem)sender;
99
+
100
+ // 表示MenuItemはToolTipプロパティにToolTipクラスが入っているから、item.ToolTipはToolTipクラス
101
+ // キャストしてContentで取り出せる
102
+ textBox.Text += ((ToolTip)item.ToolTip).Content.ToString(); // 表示 - つーるちっぷ
103
+ textBox.Text += "\n";
104
+ }
105
+ }
106
+ }
107
+ ```
108
108
  ![アプリ画像](e77966a257fb6f06fce36de9d819b45d.png)