如何使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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能