我是 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>
</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));
}