First you need a class to implement INotifyPropertyChanged or a DependencyProperty property to change the value of the property when the text of the text field changes,
namespace WpfApplication1 { public partial class MainWindow : Window, INotifyPropertyChanged { private string _test public string test { get { return _test; } set { _test = value; OnPropertyChanged("test"); } } public MainWindow() { test = "this is a test"; InitializeComponent(); } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } }
How can you bind to this property by specifying a name for this window and using the ElementName property like this.
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Name="myWindow"> <Window.Resources> <ObjectDataProvider x:Key="test"></ObjectDataProvider> </Window.Resources> <Grid> <TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding ElementName=myWindow, Path=test}" /> </Grid>
Arsen mkrtchyan
source share