31

I have a text box in WPF that is part of a datatemplate for a listbox. In that text box I can delete, backspace, spacebar, but I can NOT type in new words, letters or numbers. I CAN paste from notepad though.

What am I missing here?

 <ListBox Grid.Column="1"
         ItemsSource="{Binding Details}"
         VirtualizingStackPanel.VirtualizationMode="Recycling"
         HorizontalContentAlignment="Stretch" >
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type Entities:RADetailEntry}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <ComboBox Grid.Column="0" />
                        <TextBox Grid.Column="1" IsReadOnly="False" IsEnabled="True" 
                                 Text="{Binding Path=Description, Mode=TwoWay}" TextWrapping="Wrap"
                                 HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextAlignment="Left"  />
                    </Grid>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>
Russ
  • 12,312
  • 20
  • 59
  • 78

5 Answers5

60

I was encountering a problem very similar to this. After doing a little research, I found a similar problem listed in MSDN:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c68d5f3c-c8cc-427d-82e3-6135d075a304/

According to the answer to the post, the problem has to do with WPF and WinForms having two very different ways of handling text input. Fortunately, the post listed above gives the following solution:

When launching the window, use ElementHost.EnableModelessKeyboardInterop(window1). Note that this is a static method - you do not have to instantiate the ElementHost class.

For example,

Window window1 = new Window();
ElementHost.EnableModelessKeyboardInterop(window1);
window1.Show();

This solved the problem for me. Hope this helps.

Geoff
  • 666
  • 5
  • 3
  • 1
    Well, I like your answer better than mine. – Russ Mar 13 '09 at 12:39
  • Excellent answer. I realize you don't seem all that active, but I have started a bounty that I will award to you once I am able. – Justin Pihony Apr 24 '12 at 20:25
  • 13
    Just chiming in to mention another gotcha that can occur when you have WinForms/MFC calling WPF and using `ElementHost.EnableModelessKeyboardInterop(wpfwindow)`. If the WPF window opens another window using `Window.Show()` then the text box problem will occur there too - each individual window needs the modeless interop enabled. Note that `Window.ShowDialog()` does not cause the problem. – Aranda Aug 16 '13 at 06:59
  • Just an info if you don't find it (just because assembly is not already added). The fully qualified name is System.Windows.Forms.Integration.ElementHost() – oo_dev Jun 05 '20 at 10:31
  • Thanks! Trying to call a WPF form in Outlook and this was making me crazy. Ended up just calling myForm.ShowDialog(); instead of myForm.Show(); to fix it. – Kevin Moore Feb 08 '21 at 22:18
2

I also found the same behaviour but not when mixing wpf and win forms.

I made a custom combo box that worked fine on its own/in a little test project but when placed in the application it wasnt getting keyboard focus properly when it was clicked on.

The click was being fired but then immediately they textbox lost focus. Again you could paste things in but not type normally.

It turned out (nice one Snoop (http://snoopwpf.codeplex.com/)) that a scrollviewer that a load of combo boxes were in was stealing keyboard focus.

Marking the event as handled stopped this happening and made it work as expected:

    private void ClickOnStack(object sender, MouseButtonEventArgs e)
    {
        //do other stuff with click
        _textBox.Focus();

        //note this is key to stop things like scrollviewers nicking focus 
        e.Handled = true;
    }
JonnyRaa
  • 7,559
  • 6
  • 45
  • 49
0

Apparently one needs to add a ScrollViewer element with x:Name="PART_ContentHost" to the Border element, see the note at: http://msdn.microsoft.com/en-us/library/ms752068.aspx

0

First things first, have you noticed that there is no ItemTemplate set on your Item? second, why have you declared the DataTemplate inside a resource? are you willing to use multiple types on the ItemTemplate? if so you will need a DataTemplateSelector, that will return a specific DataTemplate for the specified type, else if you just need to add the template to this specific Item, replace the ListBox.Resources with ListBox.ItemTemplate and remove the key from the dataTemplate, compile it and there you go.

here is how it should be to work properly:

<ListBox Grid.Column="1" ItemsSource="{Binding Path=Details}" VirtualizingStackPanel.VirtualizationMode="Recycling" HorizontalContentAlignment="Stretch" >
        <!-- Remove this <ListBox.Resources> -->
        <!-- Add this -->
        <ListBox.ItemTemplate>
            <!-- Remove this <DataTemplate DataType="{x:Type Entities:RADetailEntry}"> -->
            <!-- Add this -->
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <ComboBox Grid.Column="0" />
                    <TextBox Grid.Column="1" IsReadOnly="False" IsEnabled="True" 
                        Text="{Binding Path=Description, Mode=TwoWay}" TextWrapping="Wrap"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextAlignment="Left"
                        />
                </Grid>
            </DataTemplate>
        <!-- Remove this </ListBox.Resources> -->
        <!-- Add this -->
        </ListBox.ItemTemplate>
    </ListBox>

Hopes this is still usefull since the long time from the question have been posted...

0

I created a simple test app, and I can type new text into the TextBoxes in the ListBox:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <ListBox ItemsSource="{Binding Details}"
        HorizontalAlignment="Stretch"
        VirtualizingStackPanel.VirtualizationMode="Recycling">
        <ListBox.Resources>
            <DataTemplate DataType="{x:Type app:Data}">
                <StackPanel Orientation="Horizontal">
                    <ComboBox />
                    <TextBox SpellCheck.IsEnabled="True" TextWrapping="Wrap"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                        Text="{Binding Path=Text, Mode=TwoWay}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.Resources>
    </ListBox>
</Window>

The only difference that I can see between the two is that an ItemTemplate is set on your ListBox, and one isn't on mine. What is the XAML for rADetailEntryLayout?

Andy
  • 30,088
  • 6
  • 78
  • 89
  • Maybe I am too much of a WPF noob, but I don't follow your question. The XAML is right in the datatemplate, or am I missing the question? – Russ Feb 23 '09 at 20:26
  • I tried changing to using DataType="{x:Type Entities:RADetailEntry}" instead of naming it, and it still doesn't work correctly. – Russ Feb 23 '09 at 20:27
  • You specified both an ItemTemplate, and a DataTemplate for your data type. The ItemTemplate will automatically be used for all of the items in the ListBox. Assuming all items are of type RADetailEntity, you don't need both - one or the other should do. – Andy Feb 24 '09 at 01:59
  • I specified both because I didn't know about setting the DataType="{x:Type Entities:RADetailEntry}". Without that, I had to name the datatemplate, and call it explicitly in the item template. I made the change, to be like your example, but it still fails. – Russ Feb 24 '09 at 14:46