I have the following usercontrol.
<UserControl x:Class="CommonControl.ctlTextbox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="23" d:DesignWidth="300"
Margin="0,1"
x:Name="userControl" ><DockPanel LastChildFill="True"><TextBlock DockPanel.Dock="Left" x:Name="label" TextAlignment="Right" Padding="3,0" VerticalAlignment="Center" Text="Title:"/><TextBox x:Name="textbox"
Text="{Binding ElementName=userControl, Path=Text}"
MaxLength="{Binding ElementName=userControl,Path=MaxLength}"
IsReadOnly="{Binding ElementName=userControl,Path=ReadOnly}"
IsEnabled="{Binding ElementName=userControl,Path=IsEnabled}"
Validation.ValidationAdornerSite="{Binding ElementName=userControl}"
ToolTip="{Binding ElementName=userControl,Path=ToolTip}" ></TextBox></DockPanel></UserControl>public partial class ctlTextbox : UserControl
{
#region Fields (6)
public static readonly DependencyProperty EnabledProperty = DependencyProperty.Register("Enabled", typeof(bool), typeof(ctlTextbox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true, DefaultValue = true });
public static readonly DependencyProperty LabelTextProperty = DependencyProperty.Register("LabelText", typeof(string), typeof(ctlTextbox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });
public static readonly DependencyProperty MaxLengthProperty = DependencyProperty.Register("MaxLength", typeof(int), typeof(ctlTextbox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = false });
public static readonly DependencyProperty ReadOnlyProperty = DependencyProperty.Register("ReadOnly", typeof(bool), typeof(ctlTextbox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true, DefaultValue = false });
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ctlTextbox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true});
public new static readonly DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip", typeof(object), typeof(ctlTextbox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true, DefaultValue="" });
#endregion Fields
#region Constructors (1)
public ctlTextbox()
{
InitializeComponent();
Binding labelTextBinding = new Binding("LabelText") { Source = this, Mode = BindingMode.TwoWay };
label.SetBinding(TextBlock.TextProperty, labelTextBinding);
}
#endregion Constructors
#region Properties (8)
[Browsable(true)]
public new bool IsEnabled
{
get { return (bool)GetValue(EnabledProperty); }
set { SetValue(EnabledProperty, value); }
}
[Browsable(true)]
public string LabelText
{
get { return (string)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
[Browsable(true)]
public double LabelWidth
{
get { return label.Width; }
set { label.Width = value; }
}
[Browsable(true)]
public int MaxLength
{
get { return (int)GetValue(MaxLengthProperty); }
set { SetValue(MaxLengthProperty, value); }
}
[Browsable(true)]
public bool ReadOnly
{
get { return (bool)GetValue(ReadOnlyProperty); }
set { SetValue(ReadOnlyProperty, value); }
}
[Browsable(true)]
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
[Browsable(true)]
public TextBox TextBox
{
get { return textbox; }
}
[Browsable(true)]
public new object ToolTip
{
get { return (object)GetValue(ToolTipProperty); }
set { SetValue(ToolTipProperty, value); }
}
#endregion Properties
}
}This user control is then used in a window as follows:
<Window x:Class="Server.frmLogin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:cc="clr-namespace:Server.CommonControl;assembly=Server.CommonControl"
Title="Login" Height="200" Width="320"
DataContext="{Binding Source={StaticResource Locator}, Path=EmployeeLoginViewModel}" WindowStartupLocation="CenterScreen" WindowStyle="None" Topmost="True" ResizeMode="NoResize"><DockPanel><cc:ctlTextbox Name="txtUsername" DockPanel.Dock="Top" LabelText="Username:" LabelWidth="80" Text="{Binding Username, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
MaxLength="25" /></DockPanel></Windows>
However, the update trigger is fired only on LostFocus on txtUsername in the Window. What is it that I am doing wrong?