Click here to Skip to main content
15,997,960 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This is my C# code:-
C#
namespace dynamic_inputs
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        List<string> _firstNo = new List<string>();
        int i=0;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }


        private string firstno;
        public string FirstNo
        {
            get
            {
                return firstno;                
            }
            set
            {
               if (_firstNo != null && _firstNo.Count != 0)
              {
                    
                    _firstNo.Add(value);
                    firstno = _firstNo[0];
                    
               }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            NotifyPropertyChange("FirstNo");
        }

        #region NotifyPropertyChanged

        /// <summary>
        /// Occurs when a property value changes
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raise the  <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="propertyName"></param>
        protected void NotifyPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }
}





This is my Xaml

XAML
<Window x:Class="dynamic_inputs.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">
    <Grid HorizontalAlignment="Center"
          VerticalAlignment="Center" Height="316" Width="485">
        <Grid.RowDefinitions>
            <RowDefinition Height="79*" />
            <RowDefinition Height="66*" />
            <RowDefinition Height="92*" />
            <RowDefinition Height="79*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock Text="Name: " Margin="0,0,188,42" />
        <TextBox Text="{Binding FirstNo}" Name="asd" Grid.ColumnSpan="2" Margin="89,0,0,41" />
        <Button Grid.Row="1" Margin="179,0,200,27" 
                Content="Submit"
                Click="Button_Click" Grid.ColumnSpan="2" />
        <TextBlock Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="157,16,0,0" Name="textBlock1" Text="{Binding FirstNo}" VerticalAlignment="Top" Width="156" Grid.ColumnSpan="2" />
    </Grid>
</Window>







The name is not binding to the text box. I am just a beginner. so a little confused :/

Thanks :)
Posted
Updated 25-Jul-12 1:27am
v3

1 solution

First off usually you make 2 separate objects. The reason being is it is cleaner and follows separation of concerns. What you have done is implemented INotifyPropertyChange on the actual UI class which has no purpose. The UI class has full access to its UI elements so why use extra eventing (i.e. PropertyChange)?


Secondly your setter logic is a bit odd. You are adding a value to a collection and then always setting the "return" value to be the first item. This means it will never change after an item is added.

And lastly you are not posting the property change on the setter. I see it in a button click but the button click does not do anything else.

Not sure why you have a collection but try something more simple:

C#
private string firstno;
public string FirstNo
{
    get
    {
        return firstno;
    }
    set
    {
       if (_firstNo != value)
      {
            firstno = value;
            NotifyPropertyChange(string "FirstNo");
       }
    }
}


Also, if you follow my recomendation and actually separate the objects (ViewModel and View) you want to consider what type of binding. This depends on if you are receiving the data from your view model (OneWay -> default), view (OneWayToSource), or both (TwoWay). Also there can be some cases where you use OneTime but that is rare.
Maybe review some of the stuff on MSDN to help.

http://msdn.microsoft.com/en-us/library/ms752347.aspx[^]
 
Share this answer
 
v3

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