2011-07-10 63 views
1

Please Checkout this Pictute爲什麼而更新它了源

public class Person : INotifyPropertyChanged 
{ 

    private string name; 
    // Declare the event 
    public event PropertyChangedEventHandler PropertyChanged; 
    public Person() 
    { 
    } 
    public string newPerson(string Value) 
    { 
     this.Name = Value; 
     return ""; 
    } 
    public Person(string value) 
    { 

     this.name = value; 

    } 
    public string Name 
    { 

     get { return name; } 

     set 
     { 

      name = value; 

      // Call OnPropertyChanged whenever the property is updated 

      OnPropertyChanged("Name"); 

     } 

    } 
    // Create the OnPropertyChanged method to raise the event 

    protected void OnPropertyChanged(string name) 
    { 

     PropertyChangedEventHandler handler = PropertyChanged; 

     if (handler != null) 
     { 

      handler(this, new PropertyChangedEventArgs(name)); 

     } 

    } 

不更新文本塊。 XAML:

<Window.Resources> 
    <local:Person x:Key="NewPerson" Name="shuvo"/> 
    <ObjectDataProvider x:Key="AddNewPerson" ObjectType="{x:Type local:Person}" MethodName="newPerson"> 
     <ObjectDataProvider.MethodParameters> 
      <sys:String>yahoo</sys:String> 
     </ObjectDataProvider.MethodParameters> 
    </ObjectDataProvider> 
</Window.Resources> 
<Grid> 
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="46,57,0,0" Name="textBlock1" Text="{Binding Source={StaticResource NewPerson},Path=Name}" VerticalAlignment="Top" Width="207" /> 
    <TextBox Height="23" HorizontalAlignment="Left" Margin="46,149,0,0" Name="textBox1" VerticalAlignment="Top" Width="234" Text="{Binding Source={StaticResource AddNewPerson}, Path=MethodParameters[0],BindsDirectlyToSource=True,Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged}" /> 
</Grid> 

回答

1

的ObjectDataProvider是創建類的新實例,然後調用新實例的newPerson方法。此新實例未連接到已在窗口資源中聲明爲NewPerson的實例Person實例。因此,對象數據提供者正在調用一個不起作用的方法。

你應該mofify的ObjectDataProvider的使用ObjectInstance屬性,並將其綁定到定義NewPerson的Windows資源。有關更多信息,請參閱here

相關問題