2011-01-19 65 views
6

我所要做的就是將公共屬性綁定到textBlock。我在這裏做錯了什麼?如何在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> 

回答

11

您可以簡單地添加一個DataContext並訪問你的財產

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

還要檢查這個職位的

6

起初,你需要你的類來實現INotifyPropertyChanged或屬性爲DependencyProperty對文本框的文本改變而改變的屬性值,

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

} 

比你可以通過綁定到該屬性給該窗口命名,並使用像這樣的ElementName屬性。

<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

你其實並不時使用的ObjectDataProvider

http://bea.stollnitz.com/blog/?p=22細節需要實現INotifyPropertyChanged。但是,這將是一次數據綁定。

例如在XAML:

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

在編碼:

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