[英]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.