开发者

Not able to enter text in a TextBox inside a GridView/ListView (WPF)

开发者 https://www.devze.com 2023-01-12 15:04 出处:网络
I have a TextBox and a DropDown inside a ListView which is binded to a viewModel. While comboBox is working fine, i am not able to \"enter\" any text inside the TextBox. Backspace/SpaceBar/Ctrl+C/Ctrl

I have a TextBox and a DropDown inside a ListView which is binded to a viewModel. While comboBox is working fine, i am not able to "enter" any text inside the TextBox. Backspace/SpaceBar/Ctrl+C/Ctrl+V are working just fine, but as soon as i press any alphanumeric key, it doesn;t shows any text in the TextBox.

Have also checked using empty KeyUp/KeyDown/TextChange event handler. It raises KeyUp/KeyDown event for all type of key press but TextChange is not triggered on pressing any alphanumeric key.

Please suggest if anyone has faced this problem ever. Or if there is a way to debug and find the actual problem.

This is my XAML - nothing in code behind.

<Window x:Class="Ion.MarketView.Mmi.Plugins.AlertConfigurator.View.AlertConditionItemViewDebug"
             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" 
             x:Name="aletConditionItems"
             mc:Ignorable="d" >
        <Grid >
            <Grid.Resources>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Sette开发者_开发知识库r Property="VerticalContentAlignment" Value="Stretch" />
                </Style>
            </Grid.Resources>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="75"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <ListView Grid.Column="0" x:Name="lstViewConditionItems"                     
                     ItemsSource="{Binding ElementName=aletConditionItems, Path=DataContext.Items}"                          
                     SelectedItem="{Binding ElementName=aletConditionItems, Path=DataContext.SelectedItem, Mode=TwoWay}">
                <ListView.View>
                    <GridView>
                    <GridViewColumn Header="Column Id" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox SelectedValue="{Binding Path=ColumnId}" Margin="-6,-2,-6,-2"                
                    ItemsSource="{Binding ElementName=aletConditionItems, Path=DataContext.ColumnIds}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                        <GridViewColumn Header="Value" Width="100" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="Only backSpace and SpaceBar works" ></TextBox>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>


                    </GridView>
                </ListView.View>
            </ListView>

            <StackPanel Orientation="Horizontal" Grid.Column="1" VerticalAlignment="Top" >
                <Button Content="+" Width="30" Command="{Binding ElementName=aletConditionItems, Path=DataContext.OnAddConditionItem}"></Button>
            </StackPanel>
        </Grid>
    </Window>

Thanks for your interest.

Note: 1) I am loading this WPF window from a WinForm application.

2) The problem is NOT in the binding of textBox with viewModel. I am not even able to edit a simple text that is hardcoded in the Text property (as in the code above)

3) I have not registered "any" event handler in any parent control.

Solution: As per the links suggested by @HCL, the following code fixed my problem.

Window window1 = new Window();

ElementHost.EnableModelessKeyboardInterop(window1); //This is needed for Sharing Message Loops Between Win32 and WPF 

window1.Show();


I've seen such a behaviour if a wpf-window integrated into a win-forms-application. Make a comment if it's this.

If not, check if you have registered a PreviewKeyDown-event handler in a parent-element (window, usercontrol, grid) that sets the e.Handled-property to true (or any other registered preview-event-handler that deals with keyboard-input).

Because you wrote that copy-paste and space works, I assume that you not have set TextBox.IsReadOnly in a parent-element or a style or control-template of a parent-element or of an implicit style.

One thing more: Is the line

<TextBox Text="Only backSpace and SpaceBar works" ></TextBox>

really the line that does not work or do you have a binding that not works?

Update:

Ok, because it's a win-forms-application, you must redirect the messages from the win32 message-loop. Look here, here and here.

An additonal tip: Take care on creating new windows from within an integrated WPF-window. As far as I remember from an elder project, you must also forward messages to any new child-window.

0

精彩评论

暂无评论...
验证码 换一张
取 消