小编Arp*_*ara的帖子

如何摆脱 DataContext InitializeComponent 中的 StackOverflow 异常?

我是 wpf c# 的新手,正在尝试一些示例应用程序,问题是当我DataContext在 xaml 中提到时,它InitializeComponent被递归调用并显示

mscorlib.dll 中发生 System.StackOverflowException

这是我的 XAML 标记:

<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> …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf datacontext mvvm

3
推荐指数
1
解决办法
2812
查看次数

标签 统计

c# ×1

data-binding ×1

datacontext ×1

mvvm ×1

wpf ×1