I have a button which adds a TextBox when tapped, the user can then edit the contents of the TextBox by tapping on the TextBox.
I need to show an image (with delete function) ONLY when the TextBox is in focus. i.e. Has been tapped.
The image should then disappear when the TextBox has lost focus. i.e. User has tapped on another control. Does anybody know how this can be achieved?
Below is what I have so far.
private void Textwriting_Tapped(object sender, TappedRoutedEventArgs e)
{
//Set HitTest Visibility
DragBoundsOverlay.IsHitTestVisible = false;
emojiCanvas.IsHitTestVisible = false;
textCanvas.IsHitTestVisible = true;
inkCanvas.IsHitTestVisible = false;
//TODO Add Custom Onscreen Keyboard Support
//Win Onscreen Keyboard Scope - i.e. Number, Text etc
InputScope scope = new InputScope();
InputScopeName scopeName = new InputScopeName();
scopeName.NameValue = InputScopeNameValue.ChatWithoutEmoji;
scope.Names.Add(scopeName);
if (AddTextBox == null)
{
AddTextBox = new TextBox
{
Foreground = new SolidColorBrush(Colors.White),
Background = new SolidColorBrush(Colors.Transparent),
BorderBrush = new SolidColorBrush(Colors.Transparent),
CanDrag = true,
IsTapEnabled = true,
PlaceholderText = "Type Text Here...",
FontSize = 45f,
AcceptsReturn = true,
// Win Onscreen Keyboard Settings
AllowFocusOnInteraction = true,
PreventKeyboardDisplayOnProgrammaticFocus = false,
InputScope = scope,
ManipulationMode = ManipulationModes.All,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
};
//Drag Handlers
AddTextBox.AddHandler(PointerPressedEvent, new PointerEventHandler(TextManipulation.AddTextBox_PointerPressed), true);
AddTextBox.AddHandler(PointerReleasedEvent, new PointerEventHandler(TextManipulation.AddTextBox_PointerReleased), true);
AddTextBox.AddHandler(PointerMovedEvent, new PointerEventHandler(TextManipulation.AddTextBox_PointerMoved), true);
textCanvas.Children.Add(AddTextBox);
Canvas.SetLeft(AddTextBox, 30);
Canvas.SetTop(AddTextBox, 380);
TextBoxDelete = new Image
{
Source = new BitmapImage(new Uri("ms-appx:///Resources/Elements/close.png")),
Width = 50,
Height = 50
};
TransformGroup TextBoxDelete_Transform = new TransformGroup();
TextBoxDelete_Transform.Children.Add(TextManipulation._transform);
TextBoxDelete.RenderTransform = TextBoxDelete_Transform;
textCanvas.Children.Add(TextBoxDelete);
Canvas.SetLeft(TextBoxDelete, 0);
Canvas.SetTop(TextBoxDelete, 350);
}
}
XAML
<Canvas Name="textCanvas" Width="{x:Bind DrawW}" Height="{x:Bind DrawH}" AllowDrop="True" IsHitTestVisible="False" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0">
<Canvas.RenderTransform>
<TranslateTransform X="{x:Bind DrawX}" Y="{x:Bind DrawY}"/>
</Canvas.RenderTransform>
</Canvas>
<PivotItem>
<PivotItem.Header>
<Button x:Name="TextMenu" Tapped="Textwriting_Tapped" Width="180" Height="180" Padding="0,0,0,0">
<Border BorderThickness="1,1,1,3" BorderBrush="#333333">
<Image Source="ms-appx:///Resources/textwriting.png" Stretch="UniformToFill"/>
</Border>
</Button>
</PivotItem.Header>
<Grid Height="520">
</Grid>
</PivotItem>