Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 458249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:39:09+00:00 2026-05-12T22:39:09+00:00

I have an application that uses MVVM. I have several items on the main

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:39:09+00:00Added an answer on May 12, 2026 at 10:39 pm

    You could try initialising the array when you initialise FrameworkPropertyMetaData on the dependency property.

     new FrameworkPropertyMetadata(new int [256],
                                  FrameworkPropertyMetadataOptions.AffectsRender,  
                                  new PropertyChangedCallback(ViewProperty_Changed))
    

    I think that the program might be hitting a null reference exception before it manages to bind the dependency property to the viewmodel property.


    Ok I’ve had a look at your example project and think i have a solution.

    change the int[] in the viewmodel to a List<int>.
    I’m not sure why this works. I hope there is no technical reason that list<int> is not suitable for you.

    Here is what I have changed in the solution

    in the viewmodel

    public List<int> CustomData
            {
                get
                {
                    return new List<int>(){0,1,2,3};
                }
                set
                {
                }
            }
    

    In the arraycontrol codebehind

    public static readonly DependencyProperty DataProperty =
                DependencyProperty.Register("Data",
                                            typeof(List<int>),
                                            typeof(ArrayControl),
                                            new FrameworkPropertyMetadata(new List<int>()));
    
            public List<int> Data
            {
                get { return (List<int>)GetValue(DataProperty); }
                set { SetValue(DataProperty, value); }
            }
    

    In arraycontrol.xaml. Just added listbox to show data binding working

    <UserControl x:Class="UserControlWithArray.Controls.ArrayControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
        <Grid>
                 <TextBlock x:Name="MessageTextBlock" Text="ArrayControl"/> 
            <ListBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Data}"/>
        </Grid>
    </UserControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application that uses MVVM. Properties are databound to my viewModel.
I have an application that uses window.open() to generate dynamic popups. Unfortunately, I've had
I have a WPF application implemented using the MVVM framework that uses an ActiveX
I have an application that uses a SQL Server database with several instances of
I have an MVVM application that uses a listbox which is populated with images.
I have an application that uses an HTTPWebRequest and works fine on the dev
I have an application that uses SwitchUserFilter . The switching and exiting works fine
I have an application that uses NHibernate as its ORM and sometimes it experiences
I have client application that uses WCF service to insert some data to backend
I have an application that uses the accelerometer. Sometimes, the application will launch without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.