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>