如何使Label有修改功能

如何使Label有修改功能

之前制作一个项目时需要这样一个功能: 双击Label, 随后Label变为TextBox,用户修改后回车,TextBox变回Label
之前使用WPF做了一个,代码如下:

            #region Column Header Template
            var facPanel = new FrameworkElementFactory(typeof(System.Windows.Controls.StackPanel));
            #region 设置StackPanel
            facPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
            //facPanel.SetValue(StackPanel.BackgroundProperty, Brushes.Green);
            facPanel.SetValue(StackPanel.MarginProperty, new Thickness(0, 0, 0, 0));
            //facPanel.SetValue(StackPanel.HeightProperty, (double)20);
            #endregion
            var facLabel = new FrameworkElementFactory(typeof(Label));
            var p = new Pair<Label, TextBox>(null, null);
            #region 设置Label
            //facLabel.SetValue(Label.ContentProperty, item.Description);
            var biLabel = new Binding("Description");
            biLabel.Source = item;
            biLabel.Mode = BindingMode.TwoWay;
            facLabel.SetValue(Label.ContentProperty, biLabel);
            //facLabel.SetValue(Label.FontSizeProperty, (double)10);
            //facLabel.SetValue(Label.BackgroundProperty, Brushes.Gold);
            facLabel.AddHandler(Label.MouseDoubleClickEvent, new MouseButtonEventHandler((o, a) =>
            {
                var label = (o as Label);
                if (null == label)
                    return;
                label.Visibility = Visibility.Collapsed;
            }));
            facLabel.AddHandler(Label.LoadedEvent, new RoutedEventHandler((o, a) =>
            {
                if (!(o is Label))
                    return;
                p.Key = o as Label;
                if (null != p.Key && null != p.Value)
                    __SetBindInLabelAndTextBox(p.Key, p.Value);
            }));
            #endregion
            var facTextBox = new FrameworkElementFactory(typeof(TextBox));
            #region 设置TextBox
            //var bTextBox = new Binding("Visibility") { Source=};
            //bTextBox.Mode = BindingMode.OneWay;
            facTextBox.SetValue(TextBox.VisibilityProperty, Visibility.Collapsed);
            facTextBox.AddHandler(TextBox.LoadedEvent, new RoutedEventHandler((o, a) =>
            {
                if (!(o is TextBox))
                    return;
                p.Value = o as TextBox;
                p.Value.KeyUp += new KeyEventHandler((o2, k) =>
                {
                    if (!(o2 is TextBox))
                        return;
                    if (k.Key != Key.Enter)
                        return;
                    if (p.Key == null)
                        return;
                    p.Key.Visibility = Visibility.Visible;
                });
                p.Value.LostFocus += new RoutedEventHandler((o2, r) =>
                {
                    if (!(o2 is TextBox))
                        return;
                    if (p.Key == null)
                        return;
                    p.Key.Visibility = Visibility.Visible;
                });
                p.Value.IsVisibleChanged += (sender, e) =>
                {
                    if ((bool)e.NewValue == true)
                        p.Value.SelectAll();
                    p.Value.Focus();
                };
                if (null != p.Key && null != p.Value)
                    __SetBindInLabelAndTextBox(p.Key, p.Value);
            }));
            #endregion
            var facDelBtn = new FrameworkElementFactory(typeof(Button));
            #region 设置DeleteButton
            facDelBtn.SetValue(Button.WidthProperty, (double)15);
            facDelBtn.SetValue(Button.HeightProperty, (double)15);
            facDelBtn.SetValue(Button.FontFamilyProperty, new FontFamily("Webdings"));
            facDelBtn.SetValue(Button.FontSizeProperty, (double)10);
            facDelBtn.SetValue(Button.ForegroundProperty, Brushes.Red);
            facDelBtn.SetValue(Button.BackgroundProperty, Brushes.Snow);
            facDelBtn.SetValue(Button.ContentProperty, "r");
            facDelBtn.AddHandler(Button.ClickEvent, new RoutedEventHandler((o, a) =>
            {
                _RemoveOutputColumnItem(column);
            }));
            #endregion
            #region 组合各个子控件
            facPanel.AppendChild(facLabel);
            facPanel.AppendChild(facTextBox);
            facPanel.AppendChild(facDelBtn);
            #endregion
            column.HeaderTemplate = new DataTemplate();
            column.HeaderTemplate.VisualTree = facPanel;
            #endregion

转载于:https://www.cnblogs.com/norsd/p/6359312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值