wpf – 将TextBlock绑定到Window的属性

这应该很简单,但我不能让它工作.
我有一个窗口(主要的xaml应用程序窗口)

我已经定义了一个类型为“Test”的类型(谁拥有和int ID和DateTime TestDate)

public Test CurrentTest
    {
        get
        {
            return currentTest;
        }
        set
        {
            currentTest = value;
            OnPropertyChanged("CurrentTest");
        }
    }

添加了OnPropertyChanged Impl:

public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(property));
        }
    }

现在我尝试将其绑定到窗口上的文本块.
但它不起作用:

<TextBlock Text="{Binding Source={StaticResource CurrentTest},Path=TestDate,StringFormat=dd/MM/yyyy,TargetNullValue=Not Yet Set}"></TextBlock>

这也不起作用:

<TextBlock>
            <TextBlock.Text>
                <Binding ElementName="CurrentTest" Path="TestDate" TargetNullValue="not yet set" Mode="OneWay"></Binding>
            </TextBlock.Text>
        </TextBlock>

我应该怎么做才能让textBlock显示属性的日期?

您可以使用RelativeSource属性
<TextBlock Text="{Binding Path=CurrentTest.TestDate,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}" />

相关文章

Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...
Windows文件操作基础代码 Windows下对文件进行操作使用的一段...
Winpcap基础代码 使用Winpcap进行网络数据的截获和发送都需要...
使用vbs脚本进行批量编码转换 最近需要使用SourceInsight查看...