I am trying to handle a command. In my simple application I have a textbox named txtEditor. In the code there is a problem that I do not know why it happens.
Whenever I run the following code, it executes well.
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if (txtEditor != null) e.CanExecute = (txtEditor.Text != null) && (txtEditor.SelectionLength > 0); } But for the following code:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = (txtEditor.Text != null) && (txtEditor.SelectionLength > 0); } I get this Error:
{"Object reference not set to an instance of an object."}
I have bound the command to the CommandBindings of Window Collection.
The problem is that I do not know the reason why this error happens, if txtEditor is not initialized, so what does method InitializeComponent() do in the constructor of WPF window?
And also When are the commands called that this error happens?