0
votes

I have a very odd error case that sprung up the moment I used a StaticResource converter on a Rectangle for coloring its background and at the same time using a MouseDown handler on another component next to it within a DataTemplate. If I narrow the code down a bit, this is what is required to reproduce the error :

In the top I have these resources, one pointing to a converter that takes the boolean from the binding and converts it to a fill background color):

<Window.Resources>
 <vm:DesktopViewModel x:Key="DesktopVM" />
 <vm:BooleanToColorConverter x:Key="converter" />
</Window.Resources>

And later in the same xaml file I iterate over a list of Alarm objects using this (I have replaced a Grid layout with a StackPanel and removed some other components for shorter code sample, this code snippet below still fails):

<ItemsControl ItemsSource="{Binding Alarms}">
 <ItemsControl.ItemTemplate>
   <DataTemplate>                                                        
    <StackPanel>
     <Rectangle Height="20" Stroke="Black" Width="20" RadiusX="4" RadiusY="4" Fill="{Binding Alarm, Converter={StaticResource converter}}"/>
     <Image Source="/MyNamespace;component/images/chart.png" Stretch="None" MouseDown="Image_MouseDown" Cursor="Hand"/>
    </StackPanel>
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>

If I remove the MouseDown handler on the image it runs just fine without the nullpointer error in the start. If I remove the Fill tag in the Rectangle the code works just fine WITH the MouseDown handler!!! (and the handler works just fine too). It seems like the StaticResource reference in Fill is messing up something that makes locating the mouse handler function fail?!?

Note that it fails when the window is created, not while running or clicking anything.

Edit: I had the same nullpointer issue if I replaced the converter with a style using a StaticResource with triggers to do the same as the converter. Its pretty clear that the StaticResource reference in an attribute is the culprit but I have no idea why it should affect the event listener.

Also the order of the compoents dont matter either. If I place the Image before the Rectangle the error is exactly the same.

1
A "solution" on my own problem here, I was able to use this solution to iterate over all images in the windows loaded event and attach the mousedown event programmatically. It would still be nice to know setting event listeners fails with a null reference whenever I use a static resource or style somewhere in my datatemplate.Johncl

1 Answers

1
votes

My guess is that the problem is in your converter code, that it does not take into account that it can get a null value.

Why the effect of the mouseDown? Probably it causes the rendering of the image element at an earlier moment and to request the value of the Fill property at a moment that your ViewModel has not been created yet.

There is too little information to state it with certainty, but converters that do not handle null values properly can be a major pain in WPF development in my experience. A lot of design time instability has had root in converters that did not handle the null values properly.