SetiSeeker
SetiSeeker

Reputation: 6684

WPF View not rendering on changing contentControl bound property

I am changing user controls on my main window using a bound property on a content control.

XMAL:

<ContentControl Grid.Row="0" Content="{Binding MainContent, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></ContentControl>

PROPERTY:

private UserControl _mainContent;
public UserControl MainContent
{
  get
  {
    return _mainContent;
  }
  set
  {
    _mainContent = value;
    OnPropertyChanged();
  }
}

CODE BEHIND:

MainContent = new TestUserControl();

ON PROPERTY CHANGED:

    public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
  if (this.PropertyChanged != null)
  {
    this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
  }
}

protected virtual void OnPropertyChanged()
{
  string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);
  OnPropertyChanged(propertyName);
}

My Problem is that on one specific test machine (similar to others, nothing unique or strange about it. Windows 7, 4GB Ram etc), when changing the usercontrols using this mechanism, the application hangs.

Looking at my logs, the Change command is received, the new user control is instantiated, the constructor runs. The Main Content Property is set, the OnPropertyChanged event fires

and then nothing. The application hangs and windows says its not responding and closes the app.

The OnLoaded event of the user control never gets fired.

This happens on loading any user control this way on the specific machine.

Ideas, comments are all welcome. Idea how to debug this one are welcome.

UPDATE: As this is a test machine, its not rebooted very often.

Once we rebooted the machine, the problem went away. I would still like to know why and how to stop this happening again.

PS. The Target platform is x86 and the problem machine is x64, but on the other window 7 x64 there was no issue. We are using .net framework 4.0

Upvotes: 1

Views: 1044

Answers (1)

Chris Erickson
Chris Erickson

Reputation: 161

I ran into this once before, it was caused by me using:

protected virtual void OnPropertyChanged()
{
  string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);
  OnPropertyChanged(propertyName);
}

Your properties get inlined in release mode (and perhaps during your compile) so the property name was not correctly identified by the stack search...

Upvotes: 1

Related Questions