回答編集履歴

1

見直しキャンペーン中

2023/07/18 21:47

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -1,77 +1,39 @@
1
1
  `ElementName`はビジュアルツリーから探索するため、ツリー外にいる`ContextMenu`や`DataGridColumn`からは取得できません。
2
-
3
-
4
2
 
5
3
  `x:Reference`を使用してください。
6
4
 
7
-
8
-
9
- ```xaml
5
+ ```xml
10
-
11
6
  <Window
12
-
13
7
  x:Class="Questions237500.MainWindow"
14
-
15
8
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
16
-
17
9
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
-
19
10
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
20
-
21
11
  Width="800"
22
-
23
12
  Height="450">
24
-
25
13
  <Window.Resources>
26
-
27
14
  <x:Array x:Key="items" Type="{x:Type sys:String}">
28
-
29
15
  <sys:String>a</sys:String>
30
-
31
16
  <sys:String>b</sys:String>
32
-
33
17
  </x:Array>
34
-
35
18
  </Window.Resources>
36
-
37
19
  <DockPanel>
38
-
39
20
  <StackPanel DockPanel.Dock="Top">
40
-
41
21
  <Button
42
-
43
22
  x:Name="button1"
44
-
45
23
  HorizontalAlignment="Left"
46
-
47
24
  Content="ボタン1" />
48
-
49
25
  <TextBlock Text="{Binding ElementName=button1, Path=Content}" />
50
-
51
26
  </StackPanel>
52
-
53
27
  <DataGrid AutoGenerateColumns="False" ItemsSource="{StaticResource items}">
54
-
55
28
  <DataGrid.Columns>
56
-
57
29
  <DataGridTextColumn Binding="{Binding}" Header="{Binding Source={x:Reference button1}, Path=Content}" />
58
-
59
30
  </DataGrid.Columns>
60
-
61
31
  <DataGrid.ContextMenu>
62
-
63
32
  <ContextMenu>
64
-
65
33
  <MenuItem Header="{Binding Source={x:Reference button1}, Path=Content}" />
66
-
67
34
  </ContextMenu>
68
-
69
35
  </DataGrid.ContextMenu>
70
-
71
36
  </DataGrid>
72
-
73
37
  </DockPanel>
74
-
75
38
  </Window>
76
-
77
39
  ```