wpf datagrid 如何自定义行的控件实例,(textbox 并选中则全选)

主要是为了用户输入方便

按回车,选中下一列,text自动获取焦点,输入状态

 

获取控件实例  https://blog.csdn.net/m15188153014/article/details/48627757 

textbox选中则全选  https://www.cnblogs.com/babietongtianta/p/3952214.html 

 

        private void dgContent_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGridTemplateColumn tempColumn = this.dgContent.Columns[4] as DataGridTemplateColumn;
            FrameworkElement element = this.dgContent.Columns[4].GetCellContent(this.dgContent.SelectedItem);
            if (element != null)
            {
                //把单元格元素转换为相应的控件,再从该控件中取值
                TextBox ck = tempColumn.CellTemplate.FindName("tb", element) as TextBox;
                if (ck != null)
                {
                    //if(ck.MouseDoubleClick!=null)
                    ck.PreviewMouseDown += new MouseButtonEventHandler(LIKE_textBox_PreviewMouseDown);//注意,这个事件的注册必须在LIKE_textBox获得焦点之前
                    ck.GotFocus += new RoutedEventHandler(LIKE_textBox_GotFocus);
                    ck.LostFocus += new RoutedEventHandler(LIKE_textBox_LostFocus);
                    ck.Focus();
                    ck.PreviewMouseDown -= new MouseButtonEventHandler(LIKE_textBox_PreviewMouseDown);//注意,这个事件的注册必须在LIKE_textBox获得焦点之前
                    ck.GotFocus -= new RoutedEventHandler(LIKE_textBox_GotFocus);
                    ck.LostFocus -= new RoutedEventHandler(LIKE_textBox_LostFocus);

                }  
            }
        }

        void LIKE_textBox_LostFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).PreviewMouseDown += new MouseButtonEventHandler(LIKE_textBox_PreviewMouseDown);
        }

        void LIKE_textBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            (sender as TextBox).Focus();
            e.Handled = true;
        }

        void LIKE_textBox_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
            (sender as TextBox).PreviewMouseDown -= new MouseButtonEventHandler(LIKE_textBox_PreviewMouseDown);
        }

 

转载于:https://www.cnblogs.com/swobble/p/9871627.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值