您的位置:首页 > 其它

如何使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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: