Files
MetroGauges-Old/MetroGauges/Controls/TreeNodeEdit.xaml.cs
2026-02-23 17:02:55 +08:00

260 lines
7.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
namespace MetroGauges
{
/// <summary>
/// UserControl1.xaml 的交互逻辑
/// </summary>
public partial class TreeNodeEdit : UserControl
{
public event EventHandler<NodeCheckInputArgs> NodeUpdated;
private bool isCancel = false;
//public static readonly RoutedEvent NodeUpdatedRoutedEvent =
// EventManager.RegisterRoutedEvent("NodeUpdatedHandle", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TreeNodeEdit));
//public event EventHandler NodeUpdated //自定义事件通过自定义时间为路由事件RunRoutedEvent加载、卸载方法
//{
// add { this.AddHandler(NodeUpdatedRoutedEvent, value); }
// remove { this.RemoveHandler(NodeUpdatedRoutedEvent, value); }
//}
#region Constructor
public TreeNodeEdit()
{
InitializeComponent();
base.Focusable = true;
base.FocusVisualStyle = null;
this.Loaded += new RoutedEventHandler(TreeNodeEdit_Loaded);
}
void TreeNodeEdit_Loaded(object sender, RoutedEventArgs e)
{
//if (OnlyRemark)
//{
// ContentPresenter contentpresenter = AppUtility.SearchVisualTree<ContentPresenter>(this);
// DataTemplate MyDataTemplate = contentpresenter.ContentTemplate;
// TextBlock myRemarkBlock= MyDataTemplate.FindName("txtRemark", contentpresenter) as TextBlock;
// TextBlock myTextBlock = MyDataTemplate.FindName("txtText", contentpresenter) as TextBlock;
// if (myRemarkBlock == null || myTextBlock == null) return;
// if (!string.IsNullOrWhiteSpace(myRemarkBlock.Text))
// myTextBlock.Visibility = System.Windows.Visibility.Collapsed;
//}
}
#endregion Constructor
#region Member Variables
// We keep the old text when we go into editmode
// in case the user aborts with the escape key
private string oldText;
#endregion Member Variables
#region Properties
public bool OnlyRemark
{
get
{
return (bool)GetValue(OnlyRemarkProperty);
}
set { SetValue(OnlyRemarkProperty, value); }
}
public static readonly DependencyProperty OnlyRemarkProperty =
DependencyProperty.Register(
"OnlyRemark",
typeof(bool),
typeof(TreeNodeEdit),
new PropertyMetadata(false));
public string ImageSource
{
get { return (string)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register(
"ImageSource",
typeof(string),
typeof(TreeNodeEdit),
new PropertyMetadata(""));
public string Text
{
get {
return (string)GetValue(TextProperty);
}
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text",
typeof(string),
typeof(TreeNodeEdit),
new PropertyMetadata(""));
public string Desctrition
{
get { return (string)GetValue(DesctritionProperty); }
set { SetValue(DesctritionProperty, value); }
}
public static readonly DependencyProperty DesctritionProperty =
DependencyProperty.Register(
"Desctrition",
typeof(string),
typeof(TreeNodeEdit),
new PropertyMetadata(""));
public bool IsEditable
{
get { return (bool)GetValue(IsEditableProperty); }
set { SetValue(IsEditableProperty, value); }
}
public static readonly DependencyProperty IsEditableProperty =
DependencyProperty.Register(
"IsEditable",
typeof(bool),
typeof(TreeNodeEdit),
new PropertyMetadata(true));
public bool IsInEditMode
{
get
{
if (IsEditable)
return (bool)GetValue(IsInEditModeProperty);
else
return false;
}
set
{
if (IsEditable)
{
if (value) oldText = Text;
SetValue(IsInEditModeProperty, value);
}
}
}
public static readonly DependencyProperty IsInEditModeProperty =
DependencyProperty.Register(
"IsInEditMode",
typeof(bool),
typeof(TreeNodeEdit),
new PropertyMetadata(false));
public string TextFormat
{
get { return (string)GetValue(TextFormatProperty); }
set
{
if (value == "") value = "{0}";
SetValue(TextFormatProperty, value);
}
}
public static readonly DependencyProperty TextFormatProperty =
DependencyProperty.Register(
"TextFormat",
typeof(string),
typeof(TreeNodeEdit),
new PropertyMetadata("{0}"));
public string FormattedText
{
get { return String.Format(TextFormat, Text); }
}
#endregion Properties
#region Event Handlers
// Invoked when we enter edit mode.
void TextBox_Loaded(object sender, RoutedEventArgs e)
{
TextBox txt = sender as TextBox;
// Give the TextBox input focus
txt.Focus();
txt.SelectAll();
}
// Invoked when we exit edit mode.
void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
this.IsInEditMode = false;
e.Handled = true;
if (NodeUpdated != null && !isCancel)
NodeUpdated(this, new NodeCheckInputArgs() { OldText = oldText,NewText =Text });
isCancel = false;
}
// Invoked when the user edits the annotation.
void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.IsInEditMode = false;
e.Handled = true;
}
else if (e.Key == Key.Escape)
{
this.IsInEditMode = false;
Text = oldText;
e.Handled = true;
isCancel = true;
}
}
#endregion Event Handlers
}
//定义值转换器
[ValueConversion(typeof(string), typeof(string))]
public class TextVisibleConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return "Visible";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return "";
}
}
}