如何在InitializeComponent的DataContext摆脱计算器的异常? [英] How to get rid of StackOverflow Exception in DataContext InitializeComponent?

查看:165
本文介绍了如何在InitializeComponent的DataContext摆脱计算器的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的WPF C#中,尝试一些示例应用程序,问题是,当我提到的DataContext 在XAML中的的InitializeComponent 递归调用,并呈现出




System.StackOverflowException出现在mscorlib.dll




这是我的XAML标记:

 < ;窗口x:类=Company1.MainWindow
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP://模式.microsoft.com / WinFX的/ 2006 / XAML
的xmlns:地方=CLR的命名空间:公司1
标题=主窗口HEIGHT =350WIDTH =525>
< Window.DataContext>
<局部:主窗口/>
< /Window.DataContext>
<网格和GT;
<分组框中保证金=5,5,5,5的背景=米色>
<网格和GT;
<&StackPanel的GT;
<按钮宽度=80HEIGHT =25保证金=10,10,10,10
含量=雇员命令={结合ButtonCommand}
的DataContext ={结合}>
< /按钮>
< / StackPanel的>
<数据网格
NAME =myGridView保证金=5,69,5,5
WIDTH =自动的AutoGenerateColumns =真
AlternatingRowBackground =浓汤 >
< D​​ataGrid.Columns>
< D​​ataGridTextColumn标题=名称
绑定={绑定路径= EmpName}
WIDTH =*IsReadOnly =真/>
< D​​ataGridTextColumn头=ID
绑定={绑定路径= EMPID}
WIDTH =*IsReadOnly =真/>
< D​​ataGridTextColumn标题=地方
绑定={绑定路径=位置}
WIDTH =*IsReadOnly =FALSE/>
< D​​ataGridTextColumn标题=部
绑定={绑定路径=部门}
WIDTH =*IsReadOnly =真/>
< /DataGrid.Columns>
< / DataGrid的>
< /网格和GT;
< /分组框中>
< /网格和GT;
< /窗GT;



XAML.cs



 私人ICommand的m_ButtonCommand; 
公众的ICommand ButtonCommand
{
{返回m_ButtonCommand; }
集合{m_ButtonCommand =价值; }
}
公共主窗口()
{
的InitializeComponent();
ButtonCommand =新RelayCommand(新动作<对象>(ShowEmployees));
}


解决方案

ü不需要如果您正在使用性质xaml.cs因为它是相同的部分类提供数据上下文



当您设置为MainWindow的数据上下文它创建主窗口的另一个实例并试图设置主窗口它的数据上下文。因此,要在一个无限循环给予计算器例外。



了解更多关于DataContext属性在CodeProject上的 DataContext的WPF中



如果您正在使用另一个类的浏览模式,那么你需要通过定位器提供数据上下文

 <窗​​口x:类=Company1.MainWindow
的xmlns =HTTP ://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:本地= CLR的命名空间:公司1
标题=主窗口HEIGHT =350WIDTH =525
的DataContext = {绑定路径= MainWindowViewModel,静态资源定位器}>

和定位将是一个Resources.xaml作为资源

 < MVVM:MainPageViewModelLocator X:键=定位器/> 

您可以得到有关MVVM模式的定位器类和更多的细节在geekchamp的与MVVM光强简单ViewModelLocator工作


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));
 }

解决方案

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

这篇关于如何在InitializeComponent的DataContext摆脱计算器的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆