, , , , . , , GUI Codebehind . , , , WPF.
: - , ?
: DependencyProperty, CoerceCallback // .
, - ( ) - : DataContext = "{Binding RelativeSource = {RelativeSource self}}" UserControl XAML.
XAML
<TextBox TextWrapping="Wrap"
Text="{Binding NotesText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
AcceptsReturn="True"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Disabled">
Codebehind (#)
const int MaxLineCount = 10;
const int MaxLineLength = 200;
public static readonly DependencyProperty NotesTextProperty =
DependencyProperty.Register(
name: "NotesText",
propertyType: typeof( String ),
ownerType: typeof( SampleTextBoxEntryWindow ),
typeMetadata: new PropertyMetadata(
defaultValue: string.Empty,
propertyChangedCallback: OnNotesTextPropertyChanged,
coerceValueCallback: CoerceTextLineLimiter ) );
public string NotesText
{
get { return (String)GetValue( NotesTextProperty ); }
set { SetValue( NotesTextProperty, value ); }
}
private static void OnNotesTextPropertyChanged(DependencyObject source,
DependencyPropertyChangedEventArgs e)
{
}
private static object CoerceTextLineLimiter(DependencyObject d, object value)
{
string result = null;
if (value != null)
{
string text = ((string)value);
string[] lines = text.Split( '\n' );
if (lines.Length <= MaxLineCount)
result = text;
else
{
StringBuilder obj = new StringBuilder();
for (int index = 0; index < MaxLineCount; index++)
if (lines[index].Length > 0)
obj.AppendLine( lines[index] > MaxLineLength ? lines[index].Substring(0, MaxLineLength) : lines[index] );
result = obj.ToString();
}
}
return result;
}
( , ).
, , , - , ( Regx) :
private static object CoercePhoneNumber(DependencyObject d, object value)
{
StringBuilder result = new StringBuilder();
if (value != null)
{
string text = ((string)value).ToUpper();
foreach (char chr in text)
if ((chr >= '0' && chr <= '9') || (chr == ' ') || (chr == '-') || (chr == '(') || (chr == ')'))
result.Append( chr );
}
return result.ToString();
}
, , . Coerce , - .