0

I am struggling with this issue: I'm having a MySql database that I show into a listview. Here is my code:

<ListView Name="personsListViewSearch" HorizontalAlignment="Left" Height="234" VerticalAlignment="Top" Width="592" Margin="10,105,-10,-31">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ID" Width="25"  DisplayMemberBinding="{Binding IDPerson}" />
            <GridViewColumn Header="Name:" Width="100" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="Surname" Width="160"  DisplayMemberBinding="{Binding LastName}" />
            <GridViewColumn Header="Phone" Width="150"  DisplayMemberBinding="{Binding Phone}" />
            <GridViewColumn Header="Email" Width="125"  DisplayMemberBinding="{Binding Email}" />
        </GridView>
    </ListView.View>
</ListView>

I also created a C# code for insert, delete and search, but this is not what I am looking for. I want to be able to (at run time once I have the listview) click or double click on a name of a person, edit the name and after pressing enter, save it automatically to the database. Any idea how to do that?

ataravati
  • 8,891
  • 9
  • 57
  • 89

1 Answers1

0

Exactly what @jophy job said. Use the property change event. Something like this:

public class Data : INotifyPropertyChanged
{
    // boiler-plate
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    protected bool SetField<T>(ref T field, T value, string propertyName)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    // props
    private string name;
    public string Name
    {
        get { return name; }
        set { SetField(ref name, value, "Name"); }
    }
}  

Then have each property like this:

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

This question will also lead you in the right direction:

Implementing INotifyPropertyChanged - does a better way exist?

Community
  • 1
  • 1
Code
  • 1,969
  • 3
  • 18
  • 33