Q:

Accessing TextBox inside a ContextMenu

I've got a TextBox that's part of a ContextMenu's MenuItem. However, I can't seem to access that TextBox the same way I can access other controls that are outside of the ContextMenu. Here's my 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>

The TextBox I'm trying to access is 'renameFile'.

A:

Try this code
ContextMenu cm = ItemList.Resources["listBoxItemContextMenu"] as ContextMenu;
MenuItem mi = cm.Items[0] as MenuItem;
TextBox tb = mi.Items[0] as TextBox;

