I have an application that uses MVVM. I have several items on the main window that bind to the ViewModel for that window. When I run it everything works. However, when I add a user control to the main window and try to bind to one of its dependency objects it throws an exception (“Object reference not set to an instance of an object”). The exception window just pops up on the screen and does not link to any particular place in code. And any other information in the exception is not helpful.
I’ve tried my best to trace this down but I’m not having any luck. In the constructor of window I’ve checked and verified that the item that it’s attempting to bind to exists and is an object (int[]). I’ve also manually set the property in the constructor with problems. Here are some code snippets if anyone can notice anything.
Here is where I use the user control and attempt to bind to the view property
<local:Histogram Grid.Row="2" Grid.ColumnSpan="2"
View="{Binding Path=HistogramData}"
Foreground="{DynamicResource FontColor}"
BucketStroke="{DynamicResource BucketStrokeBrush}"
BucketFill="{DynamicResource BucketFillBrush}"
SelectedBrush="{DynamicResource FamilyEditListViewSelectedBrush}"
DisabledForegroundBrush="{DynamicResource DisabledForegroundBrush}"
AxisBrush="{DynamicResource AxisBrush}"
MaxHeight="130" />
Here is the field in the view model that I am attempting to bind to:
public int[] HistogramData
{
get
{
return histogramData;
}
set
{
if (value != histogramData)
{
histogramData = value;
RaisePropertyChanged("HistogramData");
}
}
}
And in the constructor of the view model I instantiate the object
histogramData = new int[256];
And finally here is the view property in the user control
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register("View",
typeof(int[]),
typeof(Histogram),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(ViewProperty_Changed)));
public int[] View
{
get { return (int[])GetValue(ViewProperty); }
set { SetValue(ViewProperty, value); }
}
I don t know if this is enough information to solve anything so if more code is req please let me know. I could also zip up the project if someone is so inclined to look at that. Thanks in advance.