如何使Label有修改功能
2015-08-22 14:14
567 查看
如何使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
相关文章推荐
- TableView详细解释
- 能测试的主要概念和计算公式
- Java虚拟机内存分配策略
- html 标签
- Ajax提交方式
- Android dialog无标题栏样式style(for 自定义dialog)
- JavaScript中Get/Set访问器
- Jzzhu and Children
- fclose关闭stdout的问题
- LinkedBlockingQueue多线程测试
- 常见的网页布局一
- MySQL 查询空字段
- Android资源,国内镜像站点,博客文章等
- JPA基本配置
- 【Java 陷阱】SimpleDateFormat 时间格式化多线程异常
- 关于volley的一点自己的理解
- 去哪儿网自动化
- poj1905 Expanding Rods
- PPt2007制作三维立体字效果教程
- 算菜价