简体   繁体   中英

SelectedItem event fire before LostFocus to update source

I have piece of code that binds a Collection to the DataGrid, and the selection of the DataGrid will allows detail edit in the TextBoxes.

So basically DataGrid binds to a Collection, TextBoxes binds to the DataGrid.SelectedItem.(properties).

The problem here is when something editted in the TextBoxes (say add a character in textbox, without losting focus), then select another item in DataGrid (now lost focus). The DataGrid will update it SelectedItem property, but now the changes that suppose to make on the previous item is gone, the PropertyChanged event was not fire when the lost focus event happens.

I know I could solve it by using UpdateTriggerSource=PropertyChanged , but there is quite a lot of event firing around if I using PropertyChanged , so I want to see if there is a solution to solve this problem.

And below is my sample code that can reproduce this problem:

Code-Behind:

public partial class MainPage : UserControl
{
    Person _SelectedPerson { get; set; }
    public Person SelectedPerson
    {
        get
        {
            return this._SelectedPerson;
        }
        set
        {
            if (this._SelectedPerson == value)
                return;
            this._SelectedPerson = value;
        }
    }

    public MainPage()
    {
        InitializeComponent();

        Person personA = new Person() { FirstName = "Orange", LastName = "Cake" };
        Person personB = new Person() { FirstName = "Apple", LastName = "Pie" };
        ObservableCollection<Person> aPersonCollection = new ObservableCollection<Person>();
        aPersonCollection.Add(personA);
        aPersonCollection.Add(personB);

        MyDataGrid.ItemsSource = aPersonCollection;
        this.DataContext = this;
    }
}

public class Person : INotifyPropertyChanged
{
    string _LastName { get; set; }
    public string LastName
    {
        get
        {
            return this._LastName;
        }
        set
        {
            if (this._LastName == value)
                return;
            this._LastName = value;
            this.OnPropertyChanged("LastName");
        }
    }

    string _FirstName { get; set; }
    public string FirstName
    {
        get
        {
            return this._FirstName;
        }
        set {
            if (this._FirstName == value)
                return;
            this._FirstName = value;
            this.OnPropertyChanged("FirstName");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string property)
    { 
        if (PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

XAML:

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <sdk:DataGrid x:Name="MyDataGrid" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" />
        <TextBox Text="{Binding SelectedItem.FirstName, ElementName=MyDataGrid, Mode=TwoWay}" />
        <TextBox Text="{Binding SelectedItem.LastName, ElementName=MyDataGrid, Mode=TwoWay}" />
    </StackPanel>            
</Grid>

You could add and logic to the leave event of the text box that has been edited. That should fire prior to the datagrid getting focus.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM