Associating a TextBlock with a Window

It should be simple, but I can't get it to work. I have a window (main xaml application window)

I defined a property of type "Test" (which has both int ID and DateTime TestDate)

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

I added OnPropertyChanged Impl:

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

and now I'm trying to snap it to a text block in a window. But this does not work:

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

and this also does not work:

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

What should I do to make textBlock show the date of this property?

+7
source share
1 answer

You can use the RelativeSource property:

 <TextBlock Text="{Binding Path=CurrentTest.TestDate, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" /> 
+19
source

All Articles