Migration to .Net 4: null-reference exception that is added when an event is added to xaml

At my place of work, we recently updated our code base from .Net 3.5 to .Net 4 (C #). Most of the problems that were discovered have been resolved, however I cannot understand. We initialize the controls and pages through a combination of xaml and code behind (based on developer preferences), however one page throws a NullReferenceException on opening. Here is a piece of code that (one of many controls) throws.

All exceptions for code exceptions are inside the DataTemplate (I realized that this may be relevant)

<TextBox x:Name="Values" Grid.Column="1" Grid.Row="0" Margin="5,2,5,2" Text="{Binding ElementName=Descriptions, Path=SelectedValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource EmptyConverter}}" GotFocus="Column_GotFocus" MinWidth="100" CharacterCasing="Upper" Visibility="{Binding Path=IsValueVisible, Converter={StaticResource VisibilityConverter}}" /> 

Now in this case, throwing lines:

 GotFocus="Column_GotFocus" 

Column_GotFocus is located in the code behind the file. A few more facts: before the migration, we had no problems, the exception receives throws continuously, and there are three different events that cause this problem.

Three events throw:

 GotFocus="Column_GotFocus" SelectionChanged="Descriptions_SelectionChanged" Click="Search_Click" 

Removing these fixes completely fixes the problem, but obviously causes functional problems with the software. Does anyone know what might cause these problems?

EDIT:

Sorry to clarify: an event handler is never called, adding an xaml event handler (GotFocus = "Column_GotFocus", for example) where an exception is thrown.

The exception is:

 System.NullReferenceException occurred Message=Object reference not set to an instance of an object. Source= <assembly/namespace> StackTrace: at <assembly/namespace>.<Class>.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target) in <XamlFilePath>:line 291 InnerException: 

Edit 2:

Method Handler:

  private void Column_GotFocus(object sender, RoutedEventArgs e) { ContentPresenter columnContentPresenter =(DependencyObject)sender).FindParent<ContentPresenter>(); Column column = (Column)columnContentPresenter.Content; string message = string.Format("{0} ({1})", column.Name, column.Field); ModuleDescriptor.UpdateStatusBar(message); } 
+7
source share
2 answers

I think the problem is that you are trying to add event handlers during style setup. This is forbidden (for unclear reasons) ... Instead, you should use EventSetters

For example:

 <Style x:Key="YourSyleName" TargetType="{x:Type CtrlType}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="dgClient_PreviewMouseLeftButtonDown"/> <EventSetter Event="Loaded" Handler="GridContent_Loaded"/> </Style> 
+6
source

This is a structure error, and it has already been fixed. Download the hotfix to resolve this issue.

http://support.microsoft.com/kb/2464222

The error report is here:

http://social.msdn.microsoft.com/Forums/en/wpf/thread/629bfcc5-2005-4947-a001-993524798b52

Download file here.

+10
source

All Articles