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
{
///
/// UserControl1.xaml 的交互逻辑
///
public partial class TreeNodeEdit : UserControl
{
public event EventHandler 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(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 "";
}
}
}