In a window I have a textbox and I’m using a canExecute function to check if the field is empty or not. I have a to-way binding on the DependencyProperty used inside the textbox. Anyway it is always null in CanExecute (Edit : it’s not null, it’s empty : “”). Can you tell me where is the mistake ?
<TextBox Text="{Binding Path=StatusName, ElementName=Window, Mode=TwoWay}" x:Name="StatusNameTextBox" />
public String StatusName
{
get { return (String)GetValue(StatusNameProperty); }
set { SetValue(StatusNameProperty, value); }
}
public static readonly DependencyProperty StatusNameProperty =
DependencyProperty.Register("StatusName", typeof(String), typeof(AddStatusWindow), new UIPropertyMetadata(""));
private void AddNewStatusCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = !String.IsNullOrEmpty(StatusName);
}
PS : I have no binding error in the VS debogger
In the binding, set UpdateSourceTrigger=PropertyChanged. With TextBox, the default is LostFocus, so you’ll get an empty string until you tab out.