简体   繁体   中英

How to change textbox enable in a listview item by double click in WPF

I'm trying to edit a list view item content(textbox) by double click the item and I want that the listview item textbox will be enabled to edit.

this is my xaml

<ListView.View>
            <GridView >
                <GridView.Columns>
                    <GridViewColumn Header="ID" Width="50" DisplayMemberBinding="{Binding ID}"/>
                    <GridViewColumn Header="scanned Text" Width="380">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Tag="{Binding Index}" Name="itemTextBox" Text="{Binding scannedText}" BorderBrush="{x:Null}" BorderThickness="0" FontSize="16" Focusable="False">
                                </TextBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" />
            </Style>
        </ListView.ItemContainerStyle>

So I'm guessing you haven't actually tried? If you have please also post your attempts!

Add an MouseDoubleClick Event to your Textbox and try something like this in your Code:

        private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            sender.Focusable = true;
            sender.Focus();
            //These two lines prevent the Cursor to change and the border showing after clicking once and make them appear/change after the double click
            sender.Cursor = Cursors.IBeam;
            sender.BorderThickness = new Thickness(1);
        }

I suppose you want to unfocus it after editing it to so I would recommend adding a LostFocus event with this code:

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            Keyboard.ClearFocus();
            sender.Focusable = false;
            //These two lines prevent the Cursor to change and the border showing after clicking once and make them appear/change after the double click
            sender.Cursor = Cursors.Arrow;
            sender.BorderThickness = new Thickness(0);
        }

I hope this will work for the ListView

~Berdi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM