I have a DependencyObject class
public class TestDependency : DependencyObject
{
public static readonly DependencyProperty TestDateTimeProperty ...
public DateTime TestDateTime {get... set..}
}
my window is like this
public partial class MainWindow : Window{
public TestDependency td;
public MainWindow()
{
InitializeComponent();
td = new TestDependency();
td.TestDateTime = DateTime.Now;
}
}
If I wanna bind the TestDateTime property of MainWindow’s dependency object td (public TestDependency td;) to a textbox in Xaml, how do I bind it? This is what I am doing now
<TextBlock Name="tb" Text="{Binding Source = td, Path=TestDateTime, TargetNullValue=novalue}"/>
It doesn’t work at all. Anyone knows what I need to change?
First, you’ll need to define
tdas a property rather than a field, since you can only bind to properties:Then, make sure you set the data context in your window’s constructor:
Lastly, set the binding in XAML: