繁体   English   中英

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

[英]Check if selected date has been change WPF

我想将文本框中的数据保存到文件中。 但是,在用户更改日期后,我的文本框应该清除,并且应该保存数据。 有没有办法检查日期是否已更改-像布尔值这样的东西就可以了。

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>

您可以声明Nullable<DateTime>属性并将其绑定到Calendar.SelectedDate属性,并添加条件检查以确定该值是否已更改:

private Nullable<DateTime> selectedDate;
public Nullable<DateTime> SelectedDate
{
    get { return selectedDate; }
    set 
    {
        if (selectedDate != value) { /* SelectedDate has changed */ }
        selectedDate = value; 
        NotifyPropertyChanged("SelectedDate");
    }
}

...

<Calendar SelectedDate="{Binding SelectedDate}" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM