tags:

views:

1732

answers:

4

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>
A: 

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
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
I tried changing to using DataType="{x:Type Entities:RADetailEntry}" instead of naming it, and it still doesn't work correctly.
Russ
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
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
+6  A: 

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
Well, I like your answer better than mine.
Russ
A: 

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

A: 

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...

Raul Mainardi Neto