我为这个奇怪的主题措辞道歉。
我有两个日期选择器,一个仅接受数值的Duration文本框,以及SelectedDateChanged的两个方法以及Text_TextChanged。
我想要做的是MS Project用他们的开始日期/结束日期/持续时间做什么(如果有人对它有任何了解)但基本上,用户可以输入3中的2个,它将填充其他一个相应的。因此,如果我在文本框中输入3(天)并且在我的结束日期DatePicker
中选择12/10/2013,则我的2个对象应该说是startdate - 12/10/2013
结束日期 - 12/13/2013
文本3 < / p>
这是我的2个datePicker方法背后的代码。
private void DatePickerEndDate1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if(!String.IsNullOrWhiteSpace(DatePickerEndDate1.ToString()))
newEndDate = DateTime.Parse(DatePickerEndDate1.ToString());
if (!String.IsNullOrWhiteSpace(DatePickerEndDate1.ToString()) && !String.IsNullOrWhiteSpace(DatePickerStartDate1.ToString()))
{
if (DatePickerEndDate1.SelectedDate < DatePickerStartDate1.SelectedDate)
{
DatePickerStartDate1.SelectedDate = DatePickerEndDate1.SelectedDate;
DatePickerEndDate1.SelectedDate = originalStartDate;
}
TimeSpan differenceBetweenStartandEndDate = newEndDate.Subtract(newStartDate);
TextDuration.Text = differenceBetweenStartandEndDate.Days.ToString();
}
else if (DatePickerEndDate1.SelectedDate != null && DatePickerStartDate1.SelectedDate == null)
{
DatePickerStartDate1.SelectedDate = newEndDate;
DatePickerEndDate1.SelectedDate = newEndDate.AddDays(taskLength);
}
}
private void DatePickerStartDate1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if(!String.IsNullOrWhiteSpace(DatePickerStartDate1.ToString()))
newStartDate = DateTime.Parse(DatePickerStartDate1.ToString());
if (!String.IsNullOrWhiteSpace(DatePickerEndDate1.ToString()) && !String.IsNullOrWhiteSpace(DatePickerStartDate1.ToString()))
{
if (DatePickerStartDate1.SelectedDate > DatePickerEndDate1.SelectedDate)
{
DatePickerEndDate1.SelectedDate = DatePickerStartDate1.SelectedDate;
DatePickerStartDate1.SelectedDate = originalEndDate;
}
TimeSpan differenceBetweenStartandEndDate = newEndDate.Subtract(newStartDate);
TextDuration.Text = differenceBetweenStartandEndDate.Days.ToString();
}
else if (DatePickerStartDate1.SelectedDate != null && DatePickerEndDate1.SelectedDate == null)
{
DatePickerEndDate1.SelectedDate = newEndDate;
DatePickerStartDate1.SelectedDate = newEndDate.AddDays(taskLength);
}
}
我正在调试并意识到我的错误来自于我在Else if方法中重置说DatePickerStartDate1(对于此实例)。这将改变我的DatePickerEndDate1,因为它被更改,这也将调用我的TextDuration_TextChanged。
private void TextDuration_TextChanged(object sender, TextChangedEventArgs e)
{
if(int.TryParse(TextDuration.Text, out taskLength))
if (DatePickerStartDate1.SelectedDate != null)
{
DatePickerEndDate1.SelectedDate = newStartDate.AddDays(taskLength);
}
else if (DatePickerEndDate1.SelectedDate != null)
{
DatePickerStartDate1.SelectedDate = newEndDate.AddDays(taskLength);
}
}
正在解雇它,它会将我的taskLength重置为0并最终将所有日期设置为12/10/2013
,因为它从未添加3天。
现在我想知道我是否可以编写一行不会调用该方法的代码,当且仅当我在其他地方时才这样做?
答案 0 :(得分:4)
您正试图在用户界面中执行业务逻辑,这很麻烦 如果您使用日期选择器以外的其他内容更改日期,不仅会发生混乱,而且会失败 Binding,get,set和NotifyPropertyChanged是你的朋友
private DateTime? date1;
private DateTime? date2;
private Int32? taskLength;
public DateTime? Date1
{
get { return date1; }
set
{
if (date1 == value) return;
date1 = value;
NotifyPropertyChanged("Date1");
if (taskLength != null && date1 != null)
{
date2 = date1.AddDays(taskLength);
// by calling the backing property you don't hit the set
NotifyPropertyChanged("Date2");
// anything bound will call the get
}
}
}
答案 1 :(得分:3)
我过去处理这个问题的方法是使用“忽略事件”标志。代码看起来像这样:
public class MyForm
{
private bool _disableEvents = false;
private void Event1(object sender, EventArgs e)
{
if (_disableEvents)
return;
_disableEvents = true;
try
{
// do stuff that triggers Event2
}
finally
{
_disableEvents = false;
}
}
private void Event2(object sender, EventArgs e)
{
if (_disableEvents)
return;
_disableEvents = true;
try
{
// do stuff that triggers Event1
}
finally
{
_disableEvents = false;
}
}
}