How to get rid of StackOverflow Exception in DataContext InitializeComponent?
Asked Answered
D

2

3

I am new to wpf c#, trying some sample application, the problem is when I mention DataContext in xaml the InitializeComponent is called recursively and is showing

System.StackOverflowException' occurred in mscorlib.dll

This is my XAML markup:

<Window x:Class="Company1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Company1"
    Title="MainWindow" Height="350" Width="525" >
<Window.DataContext>
   <local:MainWindow/>
</Window.DataContext>
  <Grid>
     <GroupBox Margin="5,5,5,5" Background="Beige">
         <Grid>
             <StackPanel>
                <Button Width="80" Height="25" Margin="10,10,10,10" 
                        Content="Employee" Command="{Binding ButtonCommand}"
                        DataContext="{Binding }">
                </Button>
            </StackPanel>
            <DataGrid 
                  Name="myGridView" Margin="5,69,5,5" 
                  Width="Auto" AutoGenerateColumns="True"
                  AlternatingRowBackground="Bisque">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name"
                                        Binding="{Binding Path=EmpName}" 
                                        Width="*" IsReadOnly="True"/>
                    <DataGridTextColumn Header="ID" 
                                        Binding="{Binding Path=EmpId}" 
                                        Width="*" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Place" 
                                        Binding="{Binding Path=Location}" 
                                        Width="*" IsReadOnly="False"/>
                    <DataGridTextColumn Header="Dept" 
                                        Binding="{Binding Path=Department}" 
                                        Width="*" IsReadOnly="True"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </GroupBox>
</Grid>
</Window>

XAML.cs:

 private ICommand m_ButtonCommand;
 public ICommand ButtonCommand
 {
     get { return m_ButtonCommand; }
     set { m_ButtonCommand = value; }
 }
 public MainWindow()
 {
     InitializeComponent();
     ButtonCommand = new RelayCommand(new Action<object>(ShowEmployees));
 }
Degree answered 4/1, 2013 at 6:28 Comment(0)
W
5

U don't need to provide data context if you are using the properties in xaml.cs as it is the same partial class

When you set the data context as the MainWindow it creates another instance of MainWindow and tries to set its data context as MainWindow. Thus, going in an infinite loop giving stackoverflow exception.

Learn more about DataContext property in codeproject DataContext in WPF

if you are using another class for view model, then you need to provide data context via a locator

<Window x:Class="Company1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Company1"   
    Title="MainWindow" Height="350" Width="525"
    DataContext={Binding Path=MainWindowViewModel, StaticResource locator} >

and locator will be a resource in Resources.xaml as

 <MVVM:MainPageViewModelLocator x:Key="locator" />

You can get the locator class and more details about the MVVM pattern in geekchamp Working with a simple ViewModelLocator from MVVM-Light

Warlock answered 4/1, 2013 at 7:46 Comment(2)
I just came across the same problem as Arpitha, and am having trouble with your first statement. I read the MSDN and thought I wouldn't need to provide a DataContext, but I can't assign a collection as an ItemsSource for my DataGrid without it. It can only see my collection if I add <local:MainWindow /> (to either <DataGrid.DataContext> or <Window.DataContext>) or if I do it in code-behind with myDataGrid.DataContext = this;. The former breaks. The latter works. Why?Melinite
Thank you . I deleted DataContext in xaml and problem solved.Lw
O
2

The DataContext property is described as follows:

A directly embedded object that serves as data context for any bindings within the parent element. Typically, this object is a Binding or another BindingBase derived class. Alternatively, raw data of any CLR object type intended for binding may be placed here, with the actual bindings defined later.

In your XAML, the DataContext for your main window is.... your main window. So there's another instance of your main window created. Which has a DataContext of type... your main window.

So there's another instance of your main window created. Which has a DataContext of type... your main window.

So there's another instance of your main window created. Which has a DataContext of type... your main window.

So there's another instance of your main window created. Which has a DataContext of type... your main window.

So ...

;)

Set the DataContext to the object containing the data you want to bind the window against, not the window itself.

Hope this helps

Osrock answered 4/1, 2013 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.