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 8874685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:47:04+00:00 2026-06-14T18:47:04+00:00

I am having problems events raised from my viewmodel sometimes showing the datacontext as

  • 0

I am having problems events raised from my viewmodel sometimes showing the datacontext as null in the view when it is serviced. I am beginning to think this is weak binding pattern issue, and that I am not using it or am misunderstanding it (rather new to design patterns in general and started WPF a couple months ago).

Relevant MainWindow.xaml

<Grid Tree:TreeView.ItemSelected="DisplayRequested"
      Tree:TreeView.PoolCategoriesChanged="PoolContentChanged">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <GridSplitter Grid.Column="0" VerticalAlignment="Stretch"/>
    <GridSplitter Grid.Column="1" VerticalAlignment="Stretch"
                  Background="Gray" ShowsPreview="True" Width="5"
                  HorizontalAlignment="Center"/>
    <GridSplitter Grid.Column="2" VerticalAlignment="Stretch"/>
    <Tree:TreeView Name="poolTree" Grid.Column="0" Focusable="True" />
    <ScrollViewer Grid.Column="2" VerticalScrollBarVisibility="Auto">
        <Details:DetailsView Name="detailsDisplay" Focusable="True"/>
    </ScrollViewer>
</Grid>

Relevant code behind

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var vm = (CLUViewModel)e.NewValue;
        if (vm == null) return;
        vm.OwnerCleanupStarted += OnOwnerCleanupStarting;
        vm.OwnerCleanupEnded += OnOwnerCleanupEnding;
    }

    #region Event Handlers
    private void OnOwnerCleanupEnding(object sender, EventArgs e)
    {
        ViewServices.CloseProgressDialog();
    }

    private void OnOwnerCleanupStarting(object sender, EventArgs e)
    {
        var vm = DataContext as CLUViewModel;
        if (vm == null) return;
        var progressDialogViewModel = vm.ProgressDialogVM;
        ViewServices.ShowProgressDialog(GetWindow(this), progressDialogViewModel);
    }
    #endregion
}

I have several RoutedEvents that seem to function without issue. The OnOwnerCleanupStarting event seems to return null a lot on var vm = DataContext as CLUViewModel; though. Is this because it is bound too strongly and not using the WPF framework?

If I put this in debug and trace through it always works, and many times works without error during normal use. Is this a sort of race condition where I am using listeners in memory on a View that has been uninitialized when focus when to a subcomponent?

The calling logic from a VM:

public class CLUViewModel : ViewModelBase
{
    #region Properties
    private RelayCommand _manageOwnersDialogCommand;

    public ProgressDialogViewModel ProgressDialogVM;
    #endregion

    public CLUViewModel()
    {
        ProgressDialogVM = new ProgressDialogViewModel(string.Empty);
    }

    #region ManageOwnersDialogCommand
    public ICommand ManageOwnersDialogCommand
    {
        get
        {
            return _manageOwnersDialogCommand ??
                   (_manageOwnersDialogCommand = new RelayCommand(param => OnManageOwnersDialogShow()));
        }
    }

    private void OnManageOwnersDialogShow()
    {
        var dialog = new ManageOwnersDialog();
        var vm = new ManageOwnersViewModel();
        dialog.DataContext = vm;

        if (!dialog.ShowDialog().Value) return;

        var ownersRequiringCleanup = GetOwnersRequiringCleanup(vm);

        if(ownersRequiringCleanup.Count < 1) return;

        ProgressDialogVM.ClearViewModel();
        ProgressDialogVM.TokenSource = new CancellationTokenSource();
        ProgressDialogVM.ProgressMax = ownersRequiringCleanup.Count*2;

        RaiseOwnerCleanupStartedEvent();

        var taskOne = Task.Factory.StartNew(() => OwnerCleanupService.DoOwnerCleanup(ownersRequiringCleanup, ProgressDialogVM));

        taskOne.ContinueWith(t => RaiseOwnerCleanupEndedEvent(), TaskScheduler.FromCurrentSynchronizationContext());
    }

    private List<Owner> GetOwnersRequiringCleanup(ManageOwnersViewModel vm)
    {
        var ownersRequiringCleanup = new List<Owner>();

        // using DB to determine cleanup
        // Proprietary code removed for this discussion

        return ownersRequiringCleanup;
    }
    #endregion

    #region Events
    public event EventHandler OwnerCleanupStarted;
    public event EventHandler OwnerCleanupEnded;

    public void RaiseOwnerCleanupStartedEvent()
    {
        if (OwnerCleanupStarted == null) return;            
        OwnerCleanupStarted(this, new EventArgs());
    }

    public void RaiseOwnerCleanupEndedEvent()
    {
        if (OwnerCleanupEnded == null) return;
        OwnerCleanupEnded(this, new EventArgs());
    }
    #endregion
}

I have this same problem with several other controls where their various VMs make calls to Parents (within a treeview) and that parent raises the event.

I have been learning this as I go along, and some of the Events I used form the VM side was my earlier understanding of how things work. Is this where I should have called an event that raised to to my View that then starts a RountedEvent to Bubble to the appropriate level? Did I fall into a trap of strong binding vs. weak binding?

EDIT: Solved Parent/Children TreeView issue being seen. Master-Detail pattern meant I had been accessing detail Views which were not visible or never were loaded. Initial question now is still getting occurrences of null. Is there a better way to call back from VM to View for UI related issues that won’t have difficulty with View DataContext?

  • 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-06-14T18:47:05+00:00Added an answer on June 14, 2026 at 6:47 pm

    I would recommend against event-based programming models in WPF. because it’s just too easy to get into things like this. And event-based programming forces tight coupling. so its a double no-no

    Instead of throwing an event in the View, use a DelegateCommand to execute actions defined in the ViewModel level, and instead of throwing events in the ViewModel, just use regular properties and INotifyPropertyChanged to represent the state of things.

    This certainly requires a significant mindshift from the winforms or anything else non-wpf way of thinking, but when you realize it, you produce less and cleaner code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problems getting my backbone.js view events to fire. When I click
Having problems iterating. Problem has to do with const correctness, I think. I assume
I'm having problems with this query. It returns correct total_names, total_events, and total_misc, but
I am having problems using SOAP in XE/XE2 from a thread. (I didn't test
I am having problems detecting mouseover events on SVG path elements. It seems that
Like others I'm having problems getting timed events to show up on the agendaWeek
I'm having problems with getting the error: events.js:48 throw arguments[1]; // Unhandled 'error' event
I am having problems with my transition table and events. Let me explain the
I am having problems implementing a dynamic number of .live() events. There is a
Here are the problems I am having with the control from the factory: no

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.