using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SamSoft.Controls
{
public class TextSelector : Control
{
private TextBox textboxSelector;
///
/// Constructeur
///
public TextSelector()
{
this.DefaultStyleKey = typeof(TextSelector);
this.AutoSelectAll = true;
}
///
/// On selectionne tout au focus
///
public bool AutoSelectAll
{
get;
set;
}
///
/// Text
///
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(TextSelector), new PropertyMetadata(null));
///
/// Application du template
///
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.textboxSelector = this.GetTemplateChild("TextBoxSelector") as TextBox;
this.textboxSelector.KeyDown += new KeyEventHandler(textboxSelector_KeyDown);
this.textboxSelector.GotFocus += new RoutedEventHandler(textboxSelector_GotFocus);
this.textboxSelector.SelectionChanged += new RoutedEventHandler(textboxSelector_SelectionChanged);
this.textboxSelector.TextChanged += new TextChangedEventHandler(textboxSelector_TextChanged);
}
///
/// Changement de text
///
///
///
void textboxSelector_TextChanged(object sender, TextChangedEventArgs e)
{
this.textboxSelector.Text = this.Text;
this.SelectAll();
}
///
/// Texte selectionné
///
public string SelectedText
{
get;
private set;
}
///
/// Textbox interne
///
public TextBox TextBoxSelector
{
get
{
return this.textboxSelector;
}
}
///
/// On a le focus
///
///
///
void textboxSelector_GotFocus(object sender, RoutedEventArgs e)
{
this.SelectAll();
}
private void SelectAll()
{
if (this.AutoSelectAll == true)
{
this.textboxSelector.SelectAll();
}
}
///
/// On active le clavier
///
///
///
void textboxSelector_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
///
///
///
///
///
void textboxSelector_SelectionChanged(object sender, RoutedEventArgs e)
{
this.SelectAll();
this.SelectedText = this.textboxSelector.SelectedText;
}
///
/// Brush de selection
///
public Brush SelectionBackground
{
get { return (Brush)GetValue(SelectionBackgroundProperty); }
set { SetValue(SelectionBackgroundProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectionBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectionBackgroundProperty =
DependencyProperty.Register("SelectionBackground", typeof(Brush), typeof(TextSelector), null);
///
/// Brush de selection
///
public Brush SelectionForeground
{
get { return (Brush)GetValue(SelectionForegroundProperty); }
set { SetValue(SelectionForegroundProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectionForeground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectionForegroundProperty =
DependencyProperty.Register("SelectionForeground", typeof(Brush), typeof(TextSelector), null);
}
}