Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
320
WPF Textbox loses original values, even if the user doesn't udpate
posted


Hi,

I am having a very strange issue. I have a XamDatagrid which is binding to an observable collection of type Person The selected item is bind to an Object Person. I have 2 textboxes firstname and lastname. When ever user selects an item from grid, the textbox values gets populated. user can edit the values and click submit button, values gets updated.

Source to Target works correctly - i.e. able to display from viewModel When I update the values gets updated.

Lets say user selected an item with firstname john, lastname smith The problem is user edits the firstname to johnny and he doesn't click submit button instead he selects a different item from datagrid, so when I go back to the original selected item. In the grid the selected item is shown as John smith, but in the textbox the value is shown as Johnny smith.

How to solve this problem? Any help would be greatly appreciated.

  • 9836
    Suggested Answer
    posted

    You can check if your Person class implements INotifyPropertyChanged interface. This is might be the reason why the grid doesn't update the values.

    This code works fine to me:

    <igDP:XamDataGrid x:Name="xdg1"  DataSource="{Binding People}" Grid.Row="0" />

     

    <StackPanel Grid.Row="1" Orientation="Vertical">

                <TextBox Width="100" Background="Lavender"

                         Text="{Binding ElementName=xdg1, Path=ActiveDataItem.FirstName}"/>

                <TextBox Width="100" Background="Lavender"

                         Text="{Binding ElementName=xdg1, Path=ActiveDataItem.LastName}"/>

    </StackPanel>

     

    public class Person : INotifyPropertyChanged

     

        {

            private string firstName;

            private string lastName;

     

            public string FirstName

            {

                get { return firstName; }

                set

                {

                    firstName = value;

                    NotifyPropertyChanged("FirstName");

                }

            }

            public string LastName

            {

                get { return lastName; }

                set

                {

                    lastName = value;

                    NotifyPropertyChanged("LastName");

                }

            }

     

            public event PropertyChangedEventHandler PropertyChanged;

            private void NotifyPropertyChanged(string info)

            {

                if (PropertyChanged != null)

                {

                    PropertyChanged(this, new PropertyChangedEventArgs(info));

                }

            }

        }

     

    Let me know if that helps.