0

I am working on an application with TaskbarIcon from hardcodet. When I activate my CustomTrayPopup, I would like to have my CustomTextBox to be focused. The normal way within xaml doesn't seem to work.

Here's my code:

<UserControl [...]>
    <Grid x:Name="Grid">
        <Border [...]>
            [...]
        </Border>
        <TextBox [...]
                 FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"
                 > [...]
                <TextBox.Template>
                    [...]
                </TextBox.Template>
            </TextBox>
        <ListBox [...]>[...]
        </ListBox>
    </Grid>
</UserControl>

Is there some way to make the TextBox autofocus when calling the TrayPopup? Doesn't matter if xaml or code behind.

UPDATE 1: My CustomTextBox consists of an Button and the TextField. Here's whats inside of my TextBox.Template

<ControlTemplate>
  <Border [...]>
    <VisualStateManager.VisualStateGroups>[...]
    </VisualStateManager.VisualStateGroups>
    <DockPanel>
      <Button [...]>
        <Image [...]/>
        <Button.Style>[...]
        </Button.Style>
      </Button>
      <ScrollViewer Margin="0"
                    x:Name="PART_ContentHost"/>
    </DockPanel>
  </Border>
</ControlTemplate>

Is it possible, that FocusManager can not handle the button inside my TextBox?

UPDATE 2: I added a OnClick method for my Button inside the CustomTextBox, where all the Text is selected and the PART_ContentHost gets focused. But it doesn't work when i try inside the OnLoaded method oder others alike.

UPDATE 3: Here is the complete Template for my TextBox, in case it matters.

<TextBox KeyDown="SearchBox_KeyDown"
                 x:Name="SearchBox"
                 Width="160" 
                 Height="20" 
                 Margin="10,10,150,130"
                 SnapsToDevicePixels="True"
                 OverridesDefaultStyle="True"
                 Foreground="#FFFFFFFF"
                 Text="Some"
                 >

                <TextBox.CaretBrush>
                    <SolidColorBrush Color="#FF997137"/>
                </TextBox.CaretBrush>
                <TextBox.Template>
                    <ControlTemplate>
                        <Border x:Name="Border"
                            Margin="1"
                            CornerRadius="2"
                            BorderThickness="0,0,0,1.5"
                            BorderBrush="#FF997137">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                      Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                                <EasingColorKeyFrame KeyTime="0"
                                                                 Value="#FF8f8f8f"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="ReadOnly">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                      Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                                <EasingColorKeyFrame KeyTime="0"
                                                                 Value="#FF4b4b4b"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="MouseOver"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <DockPanel>
                                <Button BorderThickness="0" 
                                        DockPanel.Dock="Left" 
                                        HorizontalAlignment="Right" 
                                        Height="15" 
                                        Width="15"
                                        Background="#00ffffff"
                                        Click="Button_Click">
                                <Image Source="/Resources/searchIco.png"
                                       Margin="2"
                                       Stretch="Fill"/>
                                <Button.Style>
                                    <Style TargetType="{x:Type Button}">
                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type Button}">
                                                    <Border Background="{TemplateBinding Background}">
                                                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                    </Border>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                        <Style.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="Background" Value="#00ffffff"/>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </Button.Style>
                            </Button>
                                <ScrollViewer Margin="0"
                                              x:Name="PART_ContentHost"/>
                            </DockPanel>
                        </Border>
                    </ControlTemplate>
                </TextBox.Template>
            </TextBox>
chrosey
  • 220
  • 3
  • 15

1 Answers1

0

You can use IsVisibleChanged event to set focus on your TextBox. This sets the focus not only when the View or Usercontrol loads, but also if you a changing Views by way of Visibility. If you try to set it through the Loaded event, the event may not fire in case of hide and show. So changing the Focus of the control on VisibilityChange is helpful as it will always fire.

Here is an example to set Focus on the textbox through code behind in the UserControl

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();

            this.IsVisibleChanged += UserControl1_IsVisibleChanged;
        }

        void UserControl1_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {         
           txt.Focus();
         //txt is the name of the TextBox which is to be focused
        }
    }
Akansha
  • 933
  • 7
  • 18
  • I already tried that without success. I have some similar ButtonClick event that will work. But I don't know why. – chrosey Aug 17 '15 at 09:04
  • But this worked for me.. Let me check with the Template – Akansha Aug 17 '15 at 09:18
  • I tried with your template also. But it worked for me.. :( – Akansha Aug 17 '15 at 09:36
  • I don't know either. As I said, it works with clicking the button, but not with any other event. – chrosey Aug 17 '15 at 09:45
  • Jup. The Event is raising, but nothing happens – chrosey Aug 17 '15 at 11:31
  • Add some text to your textbox in Xaml and check whether it is displayed or not. – Akansha Aug 17 '15 at 11:53
  • i did. It is displayed. – chrosey Aug 17 '15 at 11:59
  • Make a sample application with only TextBox and its Template. It is only then you can know where the problem is exactly. Even I am unable to know the issue. May be the other controls are creating some impact. The sample may help you. – Akansha Aug 17 '15 at 12:01
  • ok I tried a sample application. and it's working fine... thats making me crazy. It seems that the TextBox is losing Focus after running the method. but not when Clicking the button. – chrosey Aug 17 '15 at 13:06
  • Method doesn't matter. Can be *_Loaded or *_VisibilityChanged. I also tried setting the Text and SelectAll(). Everythings working, but The TextBox loses Focus because of some reason – chrosey Aug 18 '15 at 07:30