This should be simple, yet I can't get it to work. I have a window (main xaml app window)
I've defined a propery of type "Test" (who has and int ID and DateTime TestDate)
public Test CurrentTest
{
get => currentTest
set
{
currentTest = value;
OnPropertyChanged("CurrentTest");
}
}
I've added the OnPropertyChanged Impl:
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
and now I try to bind it to a text block on the window. But it doesn't work:
<TextBlock Text="{Binding Source={StaticResource CurrentTest}, Path=TestDate, StringFormat=dd/MM/yyyy, TargetNullValue=Not Yet Set}"></TextBlock>
and this doesn't work either:
<TextBlock>
<TextBlock.Text>
<Binding ElementName="CurrentTest" Path="TestDate" TargetNullValue="not yet set" Mode="OneWay"></Binding>
</TextBlock.Text>
</TextBlock>
What should I do to have the textBlock show the Date of this property ?
You can use the RelativeSource property:
<TextBlock Text="{Binding Path=CurrentTest.TestDate,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With