如何在wpf中的警告框中更改警报消息

时间:2013-08-28 07:51:57

标签: wpf alertdialog

我想根据截止日期前剩余的天数在alertbox中显示消息,比如说还剩一天,警报出来并说“续订日期还有一天!”。我使用以下代码

 if ((RenewalDate.Value - DateTime.Now).TotalDays == 5)
                            MessageBox.Show("Your deadline is within 5 days");
                        else if ((RenewalDate.Value - DateTime.Now).TotalDays == 4)
                            MessageBox.Show("Your deadline is 4 days left");
                        else if ((RenewalDate.Value - DateTime.Now).TotalDays == 3)
                            MessageBox.Show("Your deadline is 3 days left");
                        else if ((RenewalDate.Value - DateTime.Now).TotalDays == 2)
                            MessageBox.Show("Your deadline is 2 days left");
                        else if ((RenewalDate.Value - DateTime.Now).TotalDays == 1)
                            MessageBox.Show("Your deadline is 1 days left");

但它不起作用。我不知道为什么。有任何想法吗?提前致谢。我的代码如下:

private int _OrganisationID = 1;
private DateTime? _RenewalDate;

public event PropertyChangedEventHandler PropertyChanged;

[Required(ErrorMessage = "OrganisationID is required.")]
public int OrganisationID
{
    get { return _OrganisationID; }
    set
    {
        if (_OrganisationID == value)
            return;

        _OrganisationID = value;
        PropertyChanged(this, new PropertyChangedEventArgs("OrganisationID"));
    }
}


[Required(ErrorMessage = "RenewalDate is a required field.")]
public DateTime? RenewalDate
{
    get { return _RenewalDate; }
    set
    {

        _RenewalDate = value;

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("RenewalDate"));
        }
    }
}


DispatcherTimer timer = new DispatcherTimer();
//  DateTime myDeadLine = new DateTime();
public void InitTimer()
{
    // Checks every minute
    timer.Interval = new TimeSpan(0, 1, 0);
    timer.Tick += timer_Tick;
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    //if (( RenewalDate.Value - DateTime.Now).TotalDays <= 1)
    //    MessageBox.Show("Your Alert Message");
}

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.Source is TabControl)
    {
        if (ClientDeadLines.IsSelected)
        {
            using (var context = new ProActiveDBEntities())
            {
                var org = context.Organisations.Where(o => o.OrganisationID == this.OrganisationID).FirstOrDefault();

                if (org != null)
                {

                    RenewalDate = org.RenewalDate;

                    if ((RenewalDate.Value - DateTime.Now).TotalDays <= 2)
                        MessageBox.Show("Your Alert Message");
                }
                else
                    {
                    MessageBox.Show("Unable to retrieve data, please set up organsiation first.");
                }
            }

        }
    }
}

1 个答案:

答案 0 :(得分:2)

您是否尝试过调试此问题?在第一行放置一个断点:

if ((RenewalDate.Value - DateTime.Now).TotalDays == 5)

((RenewalDate.Value - DateTime.Now).TotalDays等于什么

((RenewalDate.Value - DateTime.Now).TotalDays可能永远不会等于5,因为TotalDaysdouble。请尝试使用Days属性。

更新&gt;&gt;&gt;

另外,您是否知道您可以将代码重构为以下内容?:

TimeSpan daysRemaining = RenewalDate.Value - DateTime.Now;
if (daysRemaining.Days <= 5) MessageBox.Show(string.Format("Your deadline is {0} days 
away", daysRemaining.Days));