简体   繁体   English

SelectedItem 事件在 LostFocus 更新源之前触发

[英]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.我有一段代码将 Collection 绑定到 DataGrid,并且选择 DataGrid 将允许在 TextBoxes 中进行详细编辑。

So basically DataGrid binds to a Collection, TextBoxes binds to the DataGrid.SelectedItem.(properties).所以基本上 DataGrid 绑定到 Collection,TextBoxes 绑定到 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).这里的问题是当在 TextBoxes 中编辑某些内容时(比如在文本框中添加一个字符,而不会失去焦点),然后 select DataGrid 中的另一个项目(现在失去焦点)。 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. DataGrid 将更新它的 SelectedItem 属性,但现在假设对前一个项目所做的更改已经消失,当失去焦点事件发生时 PropertyChanged 事件没有触发。

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.我知道我可以通过使用UpdateTriggerSource=PropertyChanged来解决它,但是如果我使用PropertyChanged会触发很多事件,所以我想看看是否有解决这个问题的方法。

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: 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.这应该在数据网格获得焦点之前触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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