xmlns
:vmroot="clr-namespace:xxx.xx.ViewModels"------------------------------------------------------------------------------------------------------------------------
<
TextBox Name="TextBoxSource" AutomationProperties.AutomationId="PA_IssuerViews_IssuerSource_TextBox_TextBoxSource" MaxLength="2000" vmroot:ContentValidationUtility.ContentValidation="{Binding ElementName=TextBoxSource}" IsEnabled="{Binding Path=IsEnabledUpdatePanel, Mode=TwoWay}" Margin="300,60,100,0" Height="120" VerticalAlignment="Top" AcceptsReturn="True" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Visible" HorizontalAlignment="Left" Width="500"> <TextBox.Text> <Binding Path="UpdateObject.Source" Mode="TwoWay"> </Binding> </TextBox.Text> </TextBox>------------------------------------------------------------------------------------------------------------------------
public static class ContentValidationUtility
{
/// <summary>
/// Default button property dependency property
/// </summary>
public static readonly DependencyProperty TextBoxProperty
= DependencyProperty.RegisterAttached("ContentValidation", typeof(TextBox), typeof(ContentValidationUtility),
new PropertyMetadata(OnContentValidationChanged));
/// <summary>
/// Get default button
/// </summary>
public static string GetContentValidation(DependencyObject dependencyObject)
{
return (string)dependencyObject.GetValue(TextBoxProperty);
}
/// <summary>
/// Set default button
/// </summary>
public static void SetContentValidation(DependencyObject dependencyObject, Button value)
{
dependencyObject.SetValue(TextBoxProperty, value);
}
/// On default button changed
/// </summary>
private static void OnContentValidationChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
TextBox textbox = sender as TextBox;
if (textbox != null)
{
textbox.TextChanged += new TextChangedEventHandler(textboxTextChanged);
}
}
static void textboxTextChanged(object sender, TextChangedEventArgs e)
{
DependencyObject dependency = sender as DependencyObject;
object textBoxObject = dependency.GetValue(TextBoxProperty);
if (textBoxObject is TextBox)
{
TextBox textBox = textBoxObject as TextBox;
if (textBox.SelectionStart >= 0)
{
var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
if (bindingExpression != null)
bindingExpression.UpdateSource();
}
}
}
}