2011-01-19 19 views
6

Tutto ciò che sto cercando di fare è associare una proprietà pubblica a un blocco testo. Cosa sto facendo di sbagliato qui?Come database proprietà pubblica in 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> 

risposta

11

Si può semplicemente aggiungere un DataContext e accedi al tuo immobile

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

controllare anche questo post per i dettagli di quando utilizzare un ObjectDataProvider

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

6

In un primo momento è necessario classe per implementare INotifyPropertyChanged o una proprietà di essere DependencyProperty per cambiare il valore della proprietà sul cambiamento del testo textbox,

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

} 

di quanto si può associare a quella proprietà da dare un nome a quella finestra e usare la proprietà ElementName come questa.

<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

In realtà non lo fai necessario implementare INotifyPropertyChanged. Tuttavia, questo sarà un tempo vincolante per i dati.

Per esempio 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;   
    } 
Problemi correlati