wpf – 将TextBlock绑定到Window的属性

前端之家收集整理的这篇文章主要介绍了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显示属性的日期?

@H_403_24@ 您可以使用RelativeSource属性
<TextBlock Text="{Binding Path=CurrentTest.TestDate,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}" />
原文链接:https://www.f2er.com/windows/364860.html

猜你在找的Windows相关文章