locked
Stack Overflow in RaisePropertyChanged Method

    Question

  • Hello,

    I'm working on a C++ Metro style app and want to bind the Text property of a Textblock control inside a user control to a String property. The binding is simply 

    <TextBlock Text="{Binding TitleText}" />

    In the usercontrol's constructor I do this:

    InitializeComponent();
    
    m_dataContext = ref new ItemDataContext();
    this->DataContext = m_dataContext;
    
    m_dataContext->TitleText = "hallo";

    The ItemDataContext class is this one:

    [Windows::UI::Xaml::Data::Bindable]
    public ref class ItemDataContext sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    private:
    	Platform::String^ m_titleText;
    
    public:
    	ItemDataContext(void);
    	~ItemDataContext(void);
    
    	virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;
    	void RaisePropertyChanged(Platform::String^ name);
    
    	property Platform::String^ TitleText
    	{
    		Platform::String^ get() { return m_titleText; }
    		void set(Platform::String^ value) 
    		{ 
    			if (m_titleText != value)
    			{
    				m_titleText = value; 
    				RaisePropertyChanged("TitleText"); 
    			}
    		}
    	}
    }

    The RaisePropertyChanged method just does this:

    void VariationItemDataContext::RaisePropertyChanged(String^ name)
    {
        if (PropertyChanged != nullptr)
        {
            PropertyChanged(this, ref new PropertyChangedEventArgs(name));
        }
    }

    When I try to change the TitleText property in the user control's constructor (as shown in the beginning), I get this stack overflow error: "Unhandled exception at 0x7738FE9E (ntdll.dll) in MyProgram.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x03032FFC)." I looked up the callstack and the problem is that the program comes quite a lot into the RaisePropertychanged. I am confused why. The first call has "TitleText" as a String parameter (just as expected). But then it comes loads of times again into this method with this parameter: "{m_titleText=0x03984a88 L"hallo" PropertyChanged={_targets=0x00671868 } __abi_reference_count=...}".

    What am I doing wrong?

    Best regards and thanks in advance.

    Edit: I've set up an simplified project in C# with quite the same code and there it worked.

    • Edited by peterGro Monday, May 07, 2012 7:35 AM addition
    Monday, May 07, 2012 7:24 AM

Answers

  • Hello,

     

    Maybe you have the wrong implement of the RaisePropertyChange method , this function should be in the ItemDataContext class.

     

    And then, we should declare the property

     property ItemDataContext^ m_dataContext;

     

    Add the delegate function to this property

     

     m_dataContext->PropertyChanged += ref new PropertyChangedEventHandler(this, &funcname);

     

    There is a sample codes for databinding, Please check this

    http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5/

    Employee class and ScenarioOutput3 xaml.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Thursday, May 10, 2012 11:34 AM
    Wednesday, May 09, 2012 8:52 AM
  • General speaking there is no "virtual" concept in C++/CX, and the class in WinRT should be sealed.

    http://msdn.microsoft.com/en-us/library/windows/apps/br212455.aspx

     

    I think you should delete "virtual" in your codes.

     

    Best regards,

    Jesse



    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by peterGro Wednesday, May 09, 2012 9:42 AM
    Wednesday, May 09, 2012 9:33 AM

All replies

  • Hello,

     

    Maybe you have the wrong implement of the RaisePropertyChange method , this function should be in the ItemDataContext class.

     

    And then, we should declare the property

     property ItemDataContext^ m_dataContext;

     

    Add the delegate function to this property

     

     m_dataContext->PropertyChanged += ref new PropertyChangedEventHandler(this, &funcname);

     

    There is a sample codes for databinding, Please check this

    http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5/

    Employee class and ScenarioOutput3 xaml.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Thursday, May 10, 2012 11:34 AM
    Wednesday, May 09, 2012 8:52 AM
  • Thanks for your reply.

    The confusion with ItemDataContext and VariationItemDataContext was a copy & paste error. Sorry for that. So the implementation is already in the ItemDataContext class as you suggested.

    While I reviewed my code with your answer, I found the mistake: When I removed the "virtual" keyword in front of the event, it worked. However, I am not that sure whether this is the way how it should be.

    Wednesday, May 09, 2012 9:17 AM
  • General speaking there is no "virtual" concept in C++/CX, and the class in WinRT should be sealed.

    http://msdn.microsoft.com/en-us/library/windows/apps/br212455.aspx

     

    I think you should delete "virtual" in your codes.

     

    Best regards,

    Jesse



    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by peterGro Wednesday, May 09, 2012 9:42 AM
    Wednesday, May 09, 2012 9:33 AM
  • I have just noticed that intellisense marks the missing "virtual" as an error. That was the original reason why I wrote virtual. This is the message:

    "IntelliSense: 'virtual' is required to implement the matching function "Windows::UI::Xaml::Data::INotifyPropertyChanged::PropertyChanged::add" (declared in "C:\Program Files (x86)\Windows Kits\8.0\Windows Metadata\Windows.winmd")".

    I am able to compile anyway.

    • Edited by peterGro Wednesday, May 09, 2012 10:48 AM
    Wednesday, May 09, 2012 10:41 AM