Hi friends,
I'm writing a WPF search textbox inside a controls library for WinForm. Then I embed the control into a ToolStripMenuItem and everything seem to work fine until I see that the textbox behavior is like read only. I can't input any character.
Here's the code:
SearchTextBoxMenu.xaml
<UserControl x:Class="SearchTextBoxMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Margin="-34,0,-49,0">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="B2VC"/>
</UserControl.Resources>
<MenuItem>
<MenuItem.Template>
<ControlTemplate>
<Grid removed="White">
<TextBlock Name="placeHolderText" Text="Escriba aquí.." Margin="5,2,20,2" Foreground="Gray"
Visibility="{Binding ElementName=textBox,Path=Text.IsEmpty,Converter={StaticResource B2VC}}"/>
<TextBox Name="textBox" BorderBrush="Transparent" removed="Transparent" Width="150" Padding="3,0,20,0"
PreviewGotKeyboardFocus="_keyboardFocusChangedEventArgs" />
<Button Name="searchButton" Width="20" HorizontalAlignment="Right" BorderBrush="Transparent" Background="Transparent">
<Canvas >
<Path StrokeThickness="1" Stroke="Black" Height="10.167" Stretch="Fill" Width="10.167" Canvas.Left="-6.875" Canvas.Top="-6.875">
<Path.Data>
<EllipseGeometry Center="-2,-2" RadiusX="5" RadiusY="5" />
</Path.Data>
</Path>
<Path StrokeThickness="1" Stroke="Black" StrokeEndLineCap="Round" Height="5.291" Stretch="Fill" Width="5.073" Canvas.Left="1.104" Canvas.Top="1">
<Path.Data>
<LineGeometry StartPoint="1.5,1.5" EndPoint="5.5,5.5" />
</Path.Data>
</Path>
</Canvas>
</Button>
</Grid>
</ControlTemplate>
</MenuItem.Template>
</MenuItem>
</UserControl>
SearchTextBoxMenu.xaml.vb
Imports System.Windows
Imports System.Windows.Media
Public Class SearchTextBoxMenu
Private Sub _keyboardFocusChangedEventArgs(sender As Object, e As Windows.Input.KeyboardFocusChangedEventArgs)
Dim pm As UIElement = FindParent(Of Controls.ContextMenu)(sender)
If pm IsNot Nothing AndAlso pm.IsKeyboardFocusWithin Then
e.Handled = False
End If
End Sub
Private Function FindParent(Of T As DependencyObject)(child As DependencyObject) As T
Dim parentObject As DependencyObject = VisualTreeHelper.GetParent(child)
If parentObject Is Nothing Then Return Nothing
Dim parent As T = TryCast(parentObject, T)
If parent IsNot Nothing Then
Return parent
Else
Return FindParent(Of T)(parentObject)
End If
End Function
End Class
For the WinForm code, add a label which will receive the right-click:
Imports System.Windows
Public Class Form1
Dim cMenu As New Controls.ContextMenu
Sub New()
InitializeComponent()
Dim txt As New SearchTextBoxMenu
cMenu.Items.Add(txt)
txt.IsManipulationEnabled = True
End Sub
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Label1.MouseClick
cMenu.Placement = Windows.Controls.Primitives.PlacementMode.MousePoint
cMenu.IsOpen = True
End Sub
End Class
The real problem
This is how it looks over a datagridview (my final target), but it doesn't allow character input
What I have tried:
I found this
solution:
MyWpfProject.MainWindow mw = new MyWpfProject.MainWindow();
ElementHost.EnableModelessKeyboardInterop(mw);
mw.Show();
I think it doesn't work for me since I'm using a Windows Form, not a WPF Window