2011-01-19 14 views
6

Todo lo que trato de hacer es vincular una propiedad pública a un bloque de texto. ¿Qué estoy haciendo mal aquí?Cómo enlazar datos con propiedad pública en xaml

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> 

Respuesta

11

Usted puede simplemente añadir un DataContext y acceder a su propiedad

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}"/> 

También puedes ver este post para más detalles de cuándo usar un ObjectDataProvider

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

6

En un primer momento se necesita clase para implementar INotifyPropertyChanged o una propiedad para ser DependencyProperty para el cambio de valor de la propiedad sobre el cambio de texto cuadro de texto,

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)); 
     } 
    } 
} 

} 

de lo que puede obligar a que la propiedad de dando nombre a esa ventana, y usando la propiedad ElementName de esta manera.

<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> 
0

En realidad no lo hace necesidad de implementar INotifyPropertyChanged. Sin embargo, esto será un enlace de datos de una sola vez.

Por ejemplo en XAML:

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

En Código:

public string SomeProp { get; set; } 
    public MainWindow() 
    { 
     InitializeComponent(); 
     SomeProp = "Test Test Test"; 
     SomeTextBlock.DataContext = this;   
    } 
Cuestiones relacionadas