繁体   English   中英

如何通过双击 WPF 来更改列表视图项中的文本框启用

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

我正在尝试通过双击项目来编辑列表视图项目内容(文本框),并且我希望能够编辑列表视图项目文本框。

这是我的 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>

所以我猜你还没有真正尝试过? 如果您有,请也发布您的尝试!

将 MouseDoubleClick 事件添加到您的文本框并在您的代码中尝试这样的操作:

        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);
        }

我想你想在编辑后取消焦点,所以我建议使用以下代码添加一个 LostFocus 事件:

        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);
        }

我希望这适用于 ListView

~贝尔迪

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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