none
Allowing TextBox on MenuItem? RRS feed

  • Question

  • Hi there,

    I've got one ContextMenu and I'd like to have a TextBox, e.g in order to fullfil searches or getting values.

    How can we fit a TextBox?

    XAML view:

       <Separator ></Separator>
                <MenuItem  Name="Contextual_VentanaSQL" Header="{Resx _Contextual_VentanaSQL}"
                           InputGestureText="Ctrl+Ins"
                           Click="OnCommand">
                    <MenuItem.Icon>
                        <Image Source="/Imagenes/database-process-icon.png" Height="20" Width="20"/>
                    </MenuItem.Icon>

                 

                </MenuItem>

    Monday, March 17, 2014 9:11 PM

Answers

  • Since the Header property will now return a Grid object, you should set the Text property of the TextBlock inside the Grid instead of setting the Header property directly:

    <Menu>
                <MenuItem Name="Contextual_Componentes">
                    <MenuItem.Header>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock x:Name="textBlock1" Text="ContextualRecicladoComponentes" VerticalAlignment="Center"/>
                            <TextBox Margin="5 0 0 0" Width="50" Grid.Column="1"/>
                        </Grid>
                    </MenuItem.Header>
                    <MenuItem.Icon>
                        <Image Source="/Imagenes/reciclaje.png" Height="20" Width="20"/>
                    </MenuItem.Icon>
                </MenuItem>
            </Menu>
    textBlock1.Text = My.Resources.MasterGIT._ContextualRecicladoComponentes
    


    • Marked as answer by Enric Vives Tuesday, March 18, 2014 9:09 PM
    Tuesday, March 18, 2014 8:53 PM

All replies

  • It's easy to do, just not sure how you want it to show but  the following works:

                <MenuItem Header="One" Width="141">
                    <TextBox Width="100" Text="{Binding theText}"></TextBox>
                </MenuItem>
    

    The one thing you might want to play around with is the binding updatesourcetrigger since the default means the user has to tab off the menu item to get the property change.  You could put your search in the setter for the property.


    Lloyd Sheen

    Monday, March 17, 2014 9:26 PM
  • Good attempt but I'd like to have that TextBox where you can read

    "Reciclado de componentes"

    XAML stuff:


                <MenuItem Name="Contextual_Componentes" Header="{Resx _ContextualRecicladoComponentes}">
                    <TextBox Width="100"></TextBox>
                    <MenuItem.Icon>
                        <Image Source="/Imagenes/reciclaje.png" Height="20" Width="20"/>
                      
                    </MenuItem.Icon>

                </MenuItem>

    Monday, March 17, 2014 9:45 PM
  • Hi,

    this should work

    <MenuItem>
        <MenuItem.Header>
            <StackPanel Orientation="Horizontal">
                <TextBox x:Name="t1" Width="50" />
                <Button Content="Search" Click="Button_Click" />
            </StackPanel>
        </MenuItem.Header>
    </MenuItem>


    Chris

    Code Samples: Code Samples
    Chrigas Blog: Chrigas Blog

    Monday, March 17, 2014 9:53 PM
  • If you want to have the TextBox right next to the text, you could try the following markup:

                <MenuItem Name="Contextual_Componentes">
                    <MenuItem.Header>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Resx _ContextualRecicladoComponentes}" VerticalAlignment="Center"/>
                            <TextBox Margin="5 0 0 0" Width="50" Grid.Column="1"/>
                        </Grid>
                    </MenuItem.Header>
                <MenuItem.Icon>
                    <Image Source="/Imagenes/reciclaje.png" Height="20" Width="20"/>
                </MenuItem.Icon>
            </MenuItem> 
    

    Tuesday, March 18, 2014 9:49 AM
  • Magnus,

    It works properly as you see here:

    However, assigning in run-time resources' key, it doesn't and you only can see key contents

    Contextual_Componentes.Header = My.Resources.MasterGIT._ContextualRecicladoComponentes

    What the heck is happening?


    • Edited by Enric Vives Tuesday, March 18, 2014 8:33 PM
    Tuesday, March 18, 2014 8:26 PM
  • Since the Header property will now return a Grid object, you should set the Text property of the TextBlock inside the Grid instead of setting the Header property directly:

    <Menu>
                <MenuItem Name="Contextual_Componentes">
                    <MenuItem.Header>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock x:Name="textBlock1" Text="ContextualRecicladoComponentes" VerticalAlignment="Center"/>
                            <TextBox Margin="5 0 0 0" Width="50" Grid.Column="1"/>
                        </Grid>
                    </MenuItem.Header>
                    <MenuItem.Icon>
                        <Image Source="/Imagenes/reciclaje.png" Height="20" Width="20"/>
                    </MenuItem.Icon>
                </MenuItem>
            </Menu>
    textBlock1.Text = My.Resources.MasterGIT._ContextualRecicladoComponentes
    


    • Marked as answer by Enric Vives Tuesday, March 18, 2014 9:09 PM
    Tuesday, March 18, 2014 8:53 PM
  • Thanks a lot. i've got it
    Tuesday, March 18, 2014 9:09 PM
  • I would do the following. Putting the TextBox in the <MenuItem.Header> stops it going into a sub-menu:

    <MenuItem Header="_System">
        <MenuItem>
            <MenuItem.Icon>
                <TextBlock FontFamily="Segoe MDL2 Assets" Text="&#xE094;" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </MenuItem.Icon>
            <MenuItem.Header>
                <TextBox x:Name="txtFindIncident" KeyDown="txtFindIncident_KeyDown" MinWidth="55" Margin="3,0,0,0"/>
            </MenuItem.Header>
            <MenuItem.InputGestureText>
                Enter searches
            </MenuItem.InputGestureText>
        </MenuItem>
    </MenuItem>

    • Proposed as answer by Adeling Monday, May 20, 2019 1:23 PM
    Monday, May 20, 2019 1:23 PM