How to publish a database in xaml

All I'm trying to do is bind a public property to a textBlock. What am I doing wrong here?

namespace WpfApplication1 { public partial class MainWindow : Window { public string test { get; set; } public MainWindow() { test = "this is a test"; InitializeComponent(); } } } <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"> <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 Source={StaticResource test}}" /> </Grid> 

+6
wpf xaml public-key
source share
3 answers

You can simply add a datacontext and access the property

 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(); DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(String name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } } <TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding test}"/> 

Also check this post to find out when to use ObjectDataProvider

http://bea.stollnitz.com/blog/?p=22

+11
source share

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> 
+6
source share

You really don't need to implement INotifyPropertyChanged. However, this will be data binding at a time.

For example, in XAML:

 <TextBlock Name="SomeTextBlock" Text="{Binding Path=SomeProp}" /> 

In code:

  public string SomeProp { get; set; } public MainWindow() { InitializeComponent(); SomeProp = "Test Test Test"; SomeTextBlock.DataContext = this; } 
0
source share

All Articles