3

Trying to understand data binding and this totally seems like a rookie mistake but I have no idea why it's happening.

CS

namespace MuhProgram
{
    public partial class MainWindow : Window
    {
        public string foobar
        {
            get { return "loremipsum"; }
        }

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

XAML:

<Window x:Class="MuhProgram.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MuhProgram"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
         <local:MainWindow x:Key="classMainWindow"/>
    </Window.Resources>

    <Grid>
        <Label Content="{Binding Source={StaticResource classMainWindow}, Path=foobar}"></Label>
    </Grid>
</Window>

Debugger points at InitializeComponent() call in MainWindow() method with StackOverflowException.

I also tried setting DataContext attribute to "{StaticResource classMainWindow}" in the grid but the effect is the same.

1 Answer 1

7

StackOverflow exception is raised because you are recursively creating an instance of MainWindow at this line

<local:MainWindow x:Key="classMainWindow"/>

When InitializeComponent() gets called, it will initialize the XAML and load it from compiled BAML. While loading it found Label Content needs another instance of MainWindow to bind it's Content DP. Hence, it will create MainWindow recursively until it crashes with SO exception.


You don't need to declare another instance of MainWindow. Bind Label to parent instance like this:

<Label Content="{Binding Path=foobar, RelativeSource={RelativeSource FindAncestor, 
                                                            AncestorType=Window}}"/>

OR

Either set DataContext to itself and let Label inherit it from parent window.

<Window DataContext="{Binding RelativeSource={RelativeSource Self}}">
  ....
  <Label Content="{Binding foobar}"/>

OR

Set x:Name on window and bind using ElementName.

<Window x:Name="myWindow">
   .....
   <Label Content="{Binding foobar, ElementName=myWindow}" />
Sign up to request clarification or add additional context in comments.

4 Comments

What do you mean "declare another instance of MainWindow"? Where am I doing that?
Here you are declaring another instance <local:MainWindow x:Key="classMainWindow"/>.
Is this why I'm getting a stack overflow? The guy here is doing the very same thing (first XAML code snippet).
I have added more details to the answer. In the posted link, they are creating instance of another class and not of itself. Generally it is used to create instance of ViewModel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.