如何将 DateTime 绑定为 DependencyObject 的 DepedencyProperty 以便它显示在文本框中? [英] How do I bind DateTime as DepedencyProperty of a DependencyObject so that it will show up in a textbox?

查看:17
本文介绍了如何将 DateTime 绑定为 DependencyObject 的 DepedencyProperty 以便它显示在文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何使用依赖对象和属性.我创建了这个类,

I want to learn how to use Dependency Objects and Properties. I have created this class,

    public class TestDependency : DependencyObject
    {
        public static readonly DependencyProperty TestDateTimeProperty =
            DependencyProperty.Register("TestDateTime", 
            typeof(DateTime), 
            typeof(TestDependency), 
            new PropertyMetadata(DateTime.Now));

        public DateTime TestDateTime
        {
            get { return (DateTime) GetValue(TestDateTimeProperty); }
            set { SetValue(TestDateTimeProperty, value); }
        }
    }

窗口类是这样的

public partial class MainWindow : Window
{
    private TestDependency td;
    public MainWindow()
    {
        InitializeComponent();
        td = new TestDependency();
        td.TestDateTime = DateTime.Now;
    }
}

现在我想用它来显示 TextBlock 中的当前日期时间,它每秒钟,通过将其添加到网格

Now I want to use it to show a the current DateTime in the TextBlock which updates itself every second, by adding this to a grid

<Grid>
    <TextBlock Text="{Binding TestDateTime,ElementName=td}" Width="200" Height="200"/>
</Grid>

我可以看到 TextBlock,但其中根本没有日期时间值.我做错了什么?

I can see the TextBlock, but there is no Date Time value in it at all. What am I doing wrong?

推荐答案

首先,如果您想每秒更新一次显示时间,您将需要一个计时器来触发更新.DispatchTimer 可以很好地解决这个问题.

First of all if you want to update the display time once a second your going to need a timer to trigger an update. A DispatchTimer works works well for that.

public class TestDependency : DependencyObject
{
    public static readonly DependencyProperty TestDateTimeProperty =
        DependencyProperty.Register("TestDateTime", typeof(DateTime), typeof(TestDependency),
        new PropertyMetadata(DateTime.Now));

    DispatcherTimer timer;

    public TestDependency()
    {
        timer = new DispatcherTimer(new TimeSpan(0,0,1), DispatcherPriority.DataBind, new EventHandler(Callback), Application.Current.Dispatcher);
        timer.Start();

    }

    public DateTime TestDateTime
    {
        get { return (DateTime)GetValue(TestDateTimeProperty); }
        set { SetValue(TestDateTimeProperty, value); }
    }

    private void Callback(object ignore, EventArgs ex)
    {
        TestDateTime = DateTime.Now;
    }

}

接下来,我们需要修改 XAML,使其正确绑定到更新后的依赖对象.

Next we need to modify the XAML so it binds properly to the updated dependency object.

<Window.DataContext>
    <local:TestDependency/>
</Window.DataContext>
<Grid>
    <TextBlock Text="{Binding TestDateTime}" />
</Grid>

由于我们在 XAML 中设置了 DataContext,您实际上可以删除 MainWindow 构造函数中代码背后的所有代码.

Since we set the DataContext in XAML you can actually delete all of the code behind code in the MainWindow constructor.

这篇关于如何将 DateTime 绑定为 DependencyObject 的 DepedencyProperty 以便它显示在文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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