我想将文本框中的数据保存到文件中。但是在用户更改日期后我的文本框应该清晰,并且应该保存数据。有没有办法检查日期是否已经改变 - 像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>
答案 0 :(得分:0)
您可以将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}" />