0

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

    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 中的当前 DateTime ,它每秒更新一次,方法是将它添加到网格中

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

我可以看到 TextBlock,但其中根本没有 Date Time 值。我究竟做错了什么?

4

2 回答 2

2

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

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,使其正确绑定到更新的依赖对象。

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

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

于 2013-02-02T15:46:56.347 回答
0

If you just want to show some values in your TextBlock, you don't need a Dependency Object here. Try something like this:

public partial class MainWindow : Window
{
    public DateTime Test
    { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        this.Test = DateTime.Now;
    }
}

<Grid>
    <TextBlock Text="{Binding Path=Test,RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor}}"></TextBlock>
</Grid>

Here I am not showing the code which can update the value every second. I just want to clarify that this is not the right situation to use Dependency Property. Of course you can use Dependency Property to do this. But Dependency Object and Dependency Property can offer you some extension functionality such as Data Binding. But it doesn't mean that you need to use a Dependency Object or Dependency Property as the source of the Data Binding.

于 2013-02-02T15:56:51.507 回答