2

I've looked into several methods of setting focus and nothing appeared to work. I'm sure someone out there has a solution to this. It's such a simple task.

I want to set the focus of the textbox which appears in the context menu when the user right-clicks on the listbox. I don't want the user to have to click the textbox each time they right-click.

enter image description here

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox>
            <ListBox.ContextMenu>
                <ContextMenu>
                    <ContextMenu.Template>
                        <ControlTemplate>
                            <Border BorderThickness="2" BorderBrush="sc#1,.1,.1,.1" CornerRadius="4" 
                                    Background="sc#1,.05,.05,.05">

                                <TextBox Grid.Row="0" Margin="4" MinWidth="150" Name="SearchBox" VerticalAlignment="Center">
                                </TextBox>

                            </Border>
                        </ControlTemplate>
                    </ContextMenu.Template>
                </ContextMenu>
            </ListBox.ContextMenu>
        </ListBox>
    </Grid>
</Window>
JokerMartini
  • 5,674
  • 9
  • 83
  • 193
  • Not for nothing, but that's a pretty startling UI design. Just because WPF allows you to do it doesn't mean you should. Text boxes have context menus of their own- I haven't tried this but I'm pretty sure right-clicking in that text box would cause it to disappear. Are you sure that's what you want? – Chris Shain Dec 19 '15 at 01:25
  • Well if i were to modify it and make it so a usercontrol popups up when the user clicks the 'tag' key or something im fine with that. Would that help resolve the problem? – JokerMartini Dec 19 '15 at 01:27
  • I'm not sure which problem you mean to resolve. Do you mean the tab key? – Chris Shain Dec 19 '15 at 01:29
  • The problem of not being able to set the textbox in focus each time it's displayed – JokerMartini Dec 19 '15 at 01:32
  • For that you'll want to look at a WPF trigger. Many examples are available, for instance http://stackoverflow.com/questions/19225443/set-focus-on-a-textbox-control-in-usercontrol-in-wpf – Chris Shain Dec 19 '15 at 01:34

1 Answers1

1

IsFocused property of TextBox is read only. This forces the use of method in our case.

You need CallMethodAction behavior. Good tutorial to start with.

<TextBox Grid.Row="0" Margin="4" MinWidth="150" Name="SearchBox" VerticalAlignment="Center">
    <i:Interaction.Triggers>
        <ei:PropertyChangedTrigger Binding="{Binding IsOpen, RelativeSource={RelativeSource AncestorType=ContextMenu, Mode=FindAncestor}}">
            <ei:CallMethodAction MethodName="FocusSearchBox" TargetObject="{Binding DataContext, ElementName=SearchBox}"/>
            <ei:ChangePropertyAction PropertyName="Background" Value="Purple"/>
        </ei:PropertyChangedTrigger>
    </i:Interaction.Triggers>
</TextBox>




public void FocusSearchBox()
        {
            TextBox t = (TextBox) CtxMenu.ContextMenu.Template.FindName("SearchBox", CtxMenu.ContextMenu);
            t.Focus();
        }
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38