为什么DatePicker SelectedDate的默认值会导致StackOverflow异常?

时间:2016-07-20 20:54:25

标签: c# wpf xaml datepicker stack-overflow

我是WPF(和stackoverflow!)的新手,并且一直在寻找一种在DatePicker控件中设置默认日期的简单方法。我想在主视图打开时显示当前日期,并且更喜欢仅通过XAML执行此操作。

我发现的很多帖子建议使用标记扩展名和this之类的命名空间引用将静态值插入XAML,我试过没有成功,结果是StackOverflow异常:“未处理的异常在'mscorlib.dll“

中发生'System.StackOverflowException'类型

我无法弄清楚我做错了什么,或者是否有某种递归导致我的应用程序崩溃?任何和所有的帮助表示赞赏。

MainView XAML:

<Window x:Class="Tasks.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        
    xmlns:local="clr-namespace:Tasks"
    xmlns:vm="clr-namespace:Tasks.ViewModel"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d"
    Title="TASK LIST" Height="600" Width="900">
<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="20*"/>          
    </Grid.RowDefinitions>        
    <DatePicker x:Name="dpSelectedDate" SelectedDate="{x:Static sys:DateTime.Today}" />        
</Grid>

2 个答案:

答案 0 :(得分:0)

我刚测试了你的代码,对我来说它有效。 您确定StackOverflowException是由SelectedDate属性引起的吗?尝试删除Window的DataContext:

<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>

答案 1 :(得分:0)

其他错误开始出现在其他项目中,所以我决定重新映像我的工作站并重新安装Visual Studio,解决了我遇到的问题。感谢大家的意见和建议。