Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone
I am not expert in neither binding nor WPF.

I have problem in this very simple program.
I have 3 text boxes:
1- First Name
2- Surname
3- Full Name

When I change First Name or Surname, Full Name textbox does not update

this is my program. please have a look at it:


WPF:
XML
<Window x:Class="myTutorial_WPF_Binding_Example.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:WPF_Binding_Example"
        Title="MainWindow" Height="385" Width="563">
    <Window.Resources>
        <src:Person x:Key="myDataSourceObject" PersonFirstName="AAA" PersonSurName="Davis"/>
    </Window.Resources>
    <Grid>
        <TextBlock Margin="10,16,0,0" Text="First Name: " VerticalAlignment="Top" HorizontalAlignment="Left" />
        <TextBlock Margin="18,46,0,0" Text="SurName: " VerticalAlignment="Top" HorizontalAlignment="Left" />
        <TextBlock Margin="14,80,0,0" Text="Full Name: " VerticalAlignment="Top" MaxWidth="Infinity" HorizontalAlignment="Left" />
        <TextBox Margin="79,13,79,0"
                 Text="{Binding Source={StaticResource myDataSourceObject}, Path=PersonFirstName}"
                 VerticalAlignment="Top" />
        <TextBox Margin="79,43,79,0"
                 Text="{Binding Source={StaticResource myDataSourceObject}, Path=PersonSurName}"
                 VerticalAlignment="Top" />
        <TextBox Margin="79,77,79,0"
                 Text="{Binding Source={StaticResource myDataSourceObject}, Path=PersonFullName}"
                 VerticalAlignment="Top" />
    </Grid>
</Window>







and this is my class:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WPF_Binding_Example
{
    public class Person
    {
        private string _personFirstName;
        public string PersonFirstName
        {
            set
            {
                _personFirstName = value;
                PersonFullName = PersonFirstName + " " + PersonSurName;
            }
            get { return _personFirstName; }
        }
        private string _personSurName;
        public string PersonSurName
        {
            set
            {
                _personSurName = value;
                PersonFullName = PersonFirstName + " " + PersonSurName;
            }
            get { return _personSurName; }
        }
        public string PersonFullName { set; get; }
    }
}
Posted

1 solution

You need to implement INotifyPropertyChanged in your class.
Read shout this here[^].
Also see here[^].
 
Share this answer
 
Comments
[no name] 4-Aug-11 2:46am    
Thanks so much
still something is a question for me.
in this example of MSDN:

public string PhoneNumber
{
get { return this.phoneNumberValue; }

set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
NotifyPropertyChanged("PhoneNumber");
}
}
}

there is a problem. if i rename the name of property one day, since no change will happen inside the quotation, the program will face a run-time error. it may happen also when obfuscation.
Abhinav S 4-Aug-11 3:05am    
Yes you need to make sure you change the property name inside "". Find and replace might help - but there is no direct fix for that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900