Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
XML
<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
VB
Imports System.Windows
Imports System.Windows.Media

Public Class SearchTextBoxMenu
    'Avoid losing focus when mouse leaves
    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:
VB
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:
VB
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
Posted
Updated 24-May-16 8:38am
v4
Comments
Sergey Alexandrovich Kryukov 18-May-16 11:55am    
No, System.Windows.Forms solution cannot help you here, unless you implement your control as a Forms control... :-)
Will you explain what "like read only" means exactly? A picture is not the way to describe behavior.

Your UI design idea is somewhat advanced one and looks pretty nice, I like it. You just need to correct the implementation. I'll up-vote the question.

—SA
Pedro Luis Gil Mora 18-May-16 15:39pm    
I mean, the picture shows a wpf context-menu control (having the custom wpf textbox embedded) performing over the TopLeftHeaderCell and it does it well as it looks, except for the character input incapacity. Tell me how can I upload the code here and I'll do it.
Sergey Alexandrovich Kryukov 18-May-16 19:04pm    
Is it possible that you develop the highly simplified pure-WPF application (most likely, all the problem is in WPF) for the sole purpose of replicating the problem on a minimal project? It could be 2-3 files, so you could publish 100% of code. Then it will easily fit on this page. Use "Improve Question".

"character input incapacity" sounds better. Does it mean that you actually focus on the text box, press characters on the keyboards, but no text appears? I think it only justifies that the problem is pure WPF. Also, does the same happen if you don't have any code behind the control? ...Maybe I have to look at that.

—SA
Pedro Luis Gil Mora 24-May-16 14:47pm    
There you have the entire sample code. It's just to create a winform project, add a wpf custom UserControl and add the respective code. Please I'm begging you for help.
Sergey Alexandrovich Kryukov 24-May-16 15:18pm    
I see; thank you for the clarification.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900