2011-01-19 3 views
6

Tout ce que j'essaie de faire est de lier une propriété publique à un textBlock. Qu'est-ce que je fais mal ici?Comment mettre en correspondance une propriété publique dans 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> 

Répondre

11

Vous pouvez simplement ajouter un datacontext et accéder à votre propriété

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

Vérifiez également ce poste pour plus de détails quand utiliser un ObjectDataProvider

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

6

Au début, vous devez vous classe pour implémenter INotifyPropertyChanged ou une propriété d'être DependencyProperty pour changer la valeur de la propriété sur le changement de texte zone de texte,

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

} 

Que vous pouvez lier à cette propriété par donner un nom à cette fenêtre, et utiliser la propriété ElementName comme ceci.

<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

Vous ne fait pas besoin d'implémenter INotifyPropertyChanged. Cependant, il s'agira d'une liaison de données unique.

Par exemple, dans XAML:

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

Dans le code:

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