【发布时间】:2020-10-23 17:28:36
【问题描述】:
我正在使用 WPF 和 CaliburnMicro 来绑定数据。我想在每次编辑完单元格时运行一个 Add() 方法,但问题是数据网格的属性没有执行它。
这是我的代码:
<Window x:Class="DataGrid_NotifyOfPropertyChanged.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGrid_NotifyOfPropertyChanged.Views"
mc:Ignorable="d"
Title="ShellView" Height="450" Width="800">
<Grid>
<DataGrid x:Name="Numbers" CanUserAddRows="False"/>
</Grid>
public class NumbersModel
{
public int Number { get; set; }
}
ShellViewModel
public class ShellViewModel: Screen
{
public ShellViewModel()
{
Numbers.Add(new NumbersModel { Number = 1 });
Numbers.Add(new NumbersModel { Number = 2 });
}
private BindableCollection<NumbersModel> _numbers = new BindableCollection<NumbersModel>();
public BindableCollection<NumbersModel> Numbers
{
get { return _numbers; }
set {
_numbers = value;
Add();
}
}
public void Add()
{
double result = 0;
foreach(var i in _numbers.ToList())
{
result += i.Number;
}
MessageBox.Show(result.ToString());
}
}
【问题讨论】:
-
你可以把你的逻辑放在 Number 的 setter 中。您应该使用更深思熟虑的属性命名。始终在视图模型上执行 inotifypropertychanged。
标签: c# wpf datagrid caliburn.micro