我有一个TextBox,它是ContextMenu的MenuItem的一部分。但是,我似乎无法访问TextBox,就像我可以访问ContextMenu之外的其他控件一样。这是我的XAML:
<ListBox Name="ItemList">
<ListBox.Resources>
<ContextMenu x:Key="listBoxItemContextMenu">
<MenuItem Header="Rename">
<TextBox Name="newFilename" KeyUp="renameFile" />
</MenuItem>
</ContextMenu>
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu" Value="{StaticResource listBoxItemContextMenu}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
我试图访问的TextBox是'renameFile'。
答案 0 :(得分:0)
试试此代码
ContextMenu cm = ItemList.Resources["listBoxItemContextMenu"] as ContextMenu;
MenuItem mi = cm.Items[0] as MenuItem;
TextBox tb = mi.Items[0] as TextBox;