在WPF中将DateTime更新为Label [英] Updating a DateTime to a Label in WPF

查看:122
本文介绍了在WPF中将DateTime更新为Label的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对WPF很新,所以我不太清楚我在这里做错了什么。我想要当前的时间(Datetime.Now)被显示和更新到窗口上的一个标签。将调用在从数据库加载数据的方法(如最后更新的)中进行更新的时间。一旦用户登录,这个methoud被称为每2分钟(由线程上的计时器保存)。我有以下类用于时间对象...

  public class UpdatingTime:INotifyPropertyChanged 
{
public event PropertyChangedEventHandler PropertyChanged;
private DateTime _now;

public UpdatingTime()
{
_now = DateTime.Now;
}

public DateTime现在
{
get {return _now; }
private set
{
_now = value;

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

public void Update()
{
Now = DateTime.Now;
}
}

在窗口的类构造函数中,我有...

  UpdatingTime updateTime = new UpdatingTime(); 
lastUpdate.DataContext = updateTime;

在从数据库加载数据的方法中,我调用Update()方法.. 。

  updateTime.Update(); 

我认为我的数据绑定是问题,(如我所说,我很新)。我的xaml文件中的标签看起来像...

 < Label Name =lastUpdateMargin =10Height = autoContent ={Binding Source updateTime,Path = Now}
Visibility =HiddenFontSize =20/>

可见性被隐藏的原因是因为我将其设置为可见,一旦用户登录,我我已经测试了,我确定它不是问题。要清楚,后端代码是将文件中的代码放在相应的xaml文件(例如Window.xaml,Window.xaml.cs)中,所以我不认为我也缺少引用。



问题是当我运行应用程序时,根本没有显示(编译,并且没有异常被抛出)。我不知道我做错了什么,如果有人可以在这一点上说明我会非常感激的。



另外,如果你能提到一些很好的资源来学习和熟悉您发现有用的WPF,这将是非常棒的。我不知道DataContext真的是什么,如果我也正确使用它。



谢谢。

解决方案

您的内容的绑定语句有语法错误,它是错误的。
应该是:

  Content ={Binding Now}

或(相同):

  Content = {Binding Path = Now}

不需要源(如果您有多个属性绑定 - 您必须有一个逗号)。



对于WPF资源,在本网站中搜索[wpf] - 最多选票的问题是资源摘要:一个href =http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish> MVVM:从头到尾的教程?


I am fairly new to WPF so am not really sure what I am doing wrong here. I want to the current time (Datetime.Now) to be displayed and updated to a label on a window. The time would be called to update in a method that loads data from a database (like a "last updated" idea). This methoud is called every 2 minutes (kept by a timer on a thread) once the user is logged in. I have the following class for the time object...

public class UpdatingTime : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private DateTime _now;

    public UpdatingTime()
    {
        _now = DateTime.Now;
    }

    public DateTime Now
    {
        get { return _now; }
        private set
        {
            _now = value;

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

    public void Update()
    {
        Now = DateTime.Now;
    }
}

In the window's class constructor, I have...

    UpdatingTime updateTime = new UpdatingTime();
    lastUpdate.DataContext = updateTime;

In the method that loads the data from the database, I call the Update() method by...

  updateTime.Update();

I think my data binding is the problem though (like I said, I'm pretty new). My label in the xaml file looks like...

        <Label Name="lastUpdate" Margin="10" Height="auto" Content="{Binding Source updateTime, Path=Now}" 
               Visibility="Hidden" FontSize="20" />

The reason that visibility is hidden is because I set it to Visible once the user is logged in, I've tested that and I'm sure its not the issue. To be clear, the backend code is in the code behind file to the corresponding xaml file (eg Window.xaml, Window.xaml.cs), so I do not think I am missing a reference either.

The problem is that when I run the application, nothing at all displays (it compiles, and no exceptions are thrown). I am not sure what I am doing wrong, if anyone could shed some light on this I would greatly appreciate.

Also, if you could mention some good resources to learning and becoming familiar with WPF that you found helpful, that would be awesome. I am not sure what the DataContext really is and if I am using it correctly also.

Thanks.

解决方案

Your Binding statement for the Content has a syntax error, and it is wrong. should be:

Content="{Binding Now}"

or (identical):

Content="{Binding Path=Now}"

No need for 'Source' (and if you have multiple properties in Binding - you must have a comma).

For WPF resources, search in this site for [wpf] - the question with the most votes is a summary of resources: MVVM: Tutorial from start to finish?

这篇关于在WPF中将DateTime更新为Label的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆