请先看一下图片。
当用户输入数字x和mcX时,修改该单元格后,X'应该会自动计算并显示在表格中。
但是,如果我修改X和mcX的值,它将反映在表格上,并且在后面的代码上,我可以看到X'被正确修改,但是没有显示出来。它只会在我尝试修改其单元格时显示。
而且,我对数据库没有任何操作。
这是代码,
MainWindow.xaml
<DataGrid Name="PupilGrid" HeadersVisibility="All" SelectionUnit="CellOrRowHeader" AutoGenerateColumns="False"
CanUserAddRows="True" CanUserSortColumns="True" CanUserResizeColumns="True" IsReadOnly="False"
ItemsSource="{Binding}" CellEditEnding="PupilGrid_OnCellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="Frame Number" Binding="{Binding FrameNum, Mode=TwoWay}" />
<DataGridTextColumn Header="X" Binding="{Binding ObservedCenterX, Mode=TwoWay}" />
<DataGridTextColumn Header="Y" Binding="{Binding ObservedCenterY}" />
<DataGridTextColumn Header="mcX" Binding="{Binding MedicalCanthusPositionX}" />
<DataGridTextColumn Header="mcY" Binding="{Binding MedicalCanthusPositionY}" />
<DataGridTextColumn Header="X'" Binding="{Binding RelativePupilCenterX}" />
<DataGridTextColumn Header="Y'" Binding="{Binding RelativePupilCenterY}" />
</DataGrid.Columns>
</DataGrid>
MainWindow.xaml.cs
private MainViewModel vm = new MainViewModel();
public MainWindow()
{
InitializeComponent();
vm.PupilList = new ObservableCollection<Pupil>();
vm.PupilList.Add(new Pupil()
{
ObservedCenterX = 2.0,
ObservedCenterY = 1.1,
MedicalCanthusPositionX = 3.3,
MedicalCanthusPositionY = 4.2,
FrameNum = 4
});
vm.updatePupilList();
PupilGrid.ItemsSource = vm.PupilList;
}
private void PupilGrid_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
vm.updatePupilList();
}
MainViewModel.cs
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private double MedicalCanthusX = 29.02;
private double MedicalCanthusY = 16.50;
private Pupil _Model;
public Pupil Model
{
get => _Model;
set
{
if (_Model != value)
{
_Model = value;
// raise property change
// on modal change
}
}
}
private ObservableCollection<Pupil> _pupilList;
public ObservableCollection<Pupil> PupilList
{
get => _pupilList;
set
{
_pupilList = value;
Debug.WriteLine(value);
Debug.WriteLine("new value added");
updatePupilList();
PropertyChanged(this, new PropertyChangedEventArgs("RelativePupilCenterX"));
}
}
public void updatePupilList()
{
foreach (var pupil in PupilList)
{
pupil.RelativePupilCenterX = pupil.ObservedCenterX - pupil.MedicalCanthusPositionX;
pupil.RelativePupilCenterY = pupil.ObservedCenterY - pupil.MedicalCanthusPositionY;
}
}
}
Pupil.cs
public class Pupil : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private double _observedCenterX;
public double ObservedCenterX
{
get => _observedCenterX;
set
{
_observedCenterX = value;
PropertyChanged(this, new PropertyChangedEventArgs("RelativePupilCenterX"));
PropertyChanged(this, new PropertyChangedEventArgs("ObservedCenterX"));
}
}
private double _ObservedCenterY;
public double ObservedCenterY
{
get => _ObservedCenterY;
set
{
_ObservedCenterY = value;
PropertyChanged(this, new PropertyChangedEventArgs("RelativePupilCenterY"));
PropertyChanged(this, new PropertyChangedEventArgs("ObservedCenterY"));
}
}
private double _medicalCanthusPositionX;
public double MedicalCanthusPositionX
{
get => _medicalCanthusPositionX;
set
{
_medicalCanthusPositionX = value;
PropertyChanged(this, new PropertyChangedEventArgs("RelativePupilCenterX"));
PropertyChanged(this, new PropertyChangedEventArgs("MedicalCanthusPositionX"));
}
}
private double _medicalCanthusPositionY;
public double MedicalCanthusPositionY
{
get => _medicalCanthusPositionY;
set
{
_medicalCanthusPositionY = value;
PropertyChanged(this, new PropertyChangedEventArgs("RelativePupilCenterY"));
PropertyChanged(this, new PropertyChangedEventArgs("MedicalCanthusPositionY"));
}
}
private double _relativePupilCenterX;
public double RelativePupilCenterX
{
get => _relativePupilCenterX;
set
{
_relativePupilCenterX = value;
PropertyChanged(this, new PropertyChangedEventArgs("RelativePupilCenterX"));
}
}
private double _relativePupilCenterY;
public double RelativePupilCenterY
{
get => _relativePupilCenterY;
set
{
_relativePupilCenterY = value;
PropertyChanged(this, new PropertyChangedEventArgs("RelativePupilCenterY"));
}
}
public int FrameNum { get; set; }
}
答案 0 :(得分:0)
CellEditEnding ,默认情况下,TextBox的文本会在失去焦点时进行更新。 因此,在更改 X 之前,将调用 updatePupilList