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

Why does default value for DatePicker SelectedDate cause StackOverflow exception?

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

我发现的许多帖子都建议使用标记扩展和命名空间引用(如 this)将静态值插入 XAML,我尝试过但没有成功,结果是 Whosebug异常:"An unhandled exception of type 'System.WhosebugException' occurred in mscorlib.dll"

我不知道我做错了什么,或者是否有某种递归导致我的应用程序崩溃?感谢您提供任何帮助。

主视图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>

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

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

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