C# 检查所选日期是否已更改WPF

C# 检查所选日期是否已更改WPF,c#,wpf,calendar,C#,Wpf,Calendar,我想将文本框中的数据保存到文件中。但是在用户更改日期后,我的文本框应该是清晰的,并且数据应该被保存。有没有办法检查一下日期是否已经改变了——像bool这样的东西就可以了 private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e) { // ... Get reference. var calendar = sender as Calendar

我想将文本框中的数据保存到文件中。但是在用户更改日期后,我的文本框应该是清晰的,并且数据应该被保存。有没有办法检查一下日期是否已经改变了——像bool这样的东西就可以了

private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {

        // ... Get reference.
        var calendar = sender as Calendar;

        // ... See if a date is selected.
        if (calendar.SelectedDate.HasValue)
        {

            DateTime date = calendar.SelectedDate.Value;
            Stream stream = File.Open(date.ToString("MMddyyyy") + ".txt", FileMode.OpenOrCreate); // Convert the date to a legal title for a text file
            StreamWriter sw = new StreamWriter(stream);
            if(stream.Length != 0) // Check if stream is not empty 
            {
                StreamReader sr = new StreamReader(stream);
                textbox.Text = sr.ReadToEnd(); 
            }

            //sw.Write(textbox.Text);
            //sw.Dispose(); 

            // ... Display SelectedDate in Title 
            this.Title = date.ToShortDateString();
            stream.Close();
        }

        //textbox.Text = ""; 
    }
XAML代码:

<Window x:Class="Terminkalender.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Kalender" Height="350" Width="525" >
<Grid>
    <Calendar   SelectedDatesChanged="calendar_SelectedDatesChanged" Name="calendar" Background="Orange" HorizontalAlignment="Left" VerticalAlignment="Top" Height="310" Width="178" RenderTransformOrigin="0.528,0.769"/>
    <TextBox Name="textbox" AcceptsReturn="True"  HorizontalAlignment="Left" Height="149" Background="Aqua" Margin="245,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="248"/>

</Grid>


您可以在
日历中声明并数据绑定
可为null的
属性。选择EdDate
属性并添加条件检查以确定值是否已更改:

private Nullable<DateTime> selectedDate;
public Nullable<DateTime> SelectedDate
{
    get { return selectedDate; }
    set 
    {
        if (selectedDate != value) { /* SelectedDate has changed */ }
        selectedDate = value; 
        NotifyPropertyChanged("SelectedDate");
    }
}
private Nullable selectedDate;
公共可空的SelectedDate
{
获取{return selectedDate;}
设置
{
如果(selectedDate!=值){/*selectedDate已更改*/}
selectedDate=值;
NotifyPropertyChanged(“SelectedDate”);
}
}


在哪里设置日历的初始值?
<Calendar SelectedDate="{Binding SelectedDate}" />