Why is not update textblock while update it`s source
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)
{
Pr开发者_运维百科opertyChangedEventHandler 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>
The ObjectDataProvider is creating a new instance of the Person class and then calling the newPerson method of that new instance. This new instance is not connected to the already existing Person instance that you declared in the window resources as NewPerson. Therefore the object data provider is calling a method that has no effect.
You should mofify the ObjectDataProvider to use the ObjectInstance property and bind it to the windows resources defined NewPerson. See here for more information.
精彩评论