gpt4 book ai didi

c# - MVVM 中的 Datagrid 绑定(bind)和编辑

转载 作者:行者123 更新时间:2023-12-03 10:44:19 28 4
gpt4 key购买 nike

在使用了多年的代码后,我刚刚尝试从 MVVM 开始。我试图让 ThisClaimValue 在 PercentageComplete 更改时更新。 ThisClaimValue 旨在显示 ContractAmt 的百分比,基于 PercentageComplete。无论是在人们投入值(value)的时候,还是在他们离开牢房的时候。我正在尝试使用零代码来执行此操作,因此没有内置事件。

我将 EF Database First 用于描述、ContractAmt 和 BillCurrentAmt。 PercentageComplete 和 ThisClaimValue 作为 EF 创建的类的部分类位于单独的解决方案中。

看法:

   <DataGrid Margin="10,10,10,0" RowDetailsVisibilityMode="VisibleWhenSelected" EnableRowVirtualization="True" AutoGenerateColumns="False" ItemsSource="{Binding JCCISelectedList, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding JCCI}" Grid.Row="3">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Description}" Header="Description" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding ContractAmt}" Header="Value" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding PercentageComplete, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="% Complete" Width="Auto"/>
<DataGridTextColumn Binding="{Binding BillCurrentAmt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="$ Complete" Width="Auto"/>
<DataGridTextColumn Binding="{Binding ThisClaimValue}" Header="This Claim Value" Width="Auto" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>

View 模型:
  public class JBProgressBillItemsViewModel : INotifyPropertyChanged, IPageViewModel
{
public AcceptCommand AcceptEvent { get; set; }
public BackCommand BackEvent { get; set; }
public string Name => "JBProgressBillItems";

public JBProgressBillItemsViewModel()
{
HQCOList = Facade.GetVistaHQCO();
AcceptEvent = new AcceptCommand(this);
BackEvent = new BackCommand(this);
}
private bHQCO _hqco;
private bJCCM _jccm;
private bJCCI _jcciSection;
private bJCCI _jcci;

public ObservableCollection<bHQCO> HQCOList { get; }
public ObservableCollection<bJCCM> JCCMList { get; private set; }
public ObservableCollection<bJCCI> JCCISectionList { get; private set; }
public ObservableCollection<bJCCI> JCCIList { get; private set; }
public ObservableCollection<bJCCI> JCCISelectedList { get; private set; }

public bHQCO HQCO
{
get { return _hqco; }
set
{
_hqco = value;
JCCMList = Facade.GetVistaActiveProjects(_hqco.HQCo);
RaisePropertyChanged(nameof(HQCO));
RaisePropertyChanged(nameof(JCCMList));
}
}

public bJCCM JCCM
{
get { return _jccm; }
set
{
_jccm = value;
JCCIList = Facade.GetVistaContractItems(_hqco.HQCo, _jccm.Contract);
JCCISectionList =
new ObservableCollection<bJCCI>(JCCIList.Where(x => x.SICode == "H" || x.SICode == "SH"));
RaisePropertyChanged(nameof(JCCM));
RaisePropertyChanged(nameof(JCCISectionList));
}
}

public bJCCI JCCISection
{
get { return _jcciSection; }
set
{
_jcciSection = value;
try
{
JCCISelectedList = new ObservableCollection<bJCCI>(JCCIList.Where(x => _jcciSection.BillGroup == x.BillGroup && string.IsNullOrWhiteSpace(x.SICode)));
}
catch (ArgumentNullException)
{
}
RaisePropertyChanged(nameof(JCCISection));
RaisePropertyChanged(nameof(JCCISelectedList));
}
}

public bJCCI JCCI
{
get { return _jcci; }
set
{
_jcci = value;
_jcci.ThisClaimValue = value.PercentageComplete * value.ContractAmt / 100;
RaisePropertyChanged(nameof(JCCI));
RaisePropertyChanged(nameof(JCCISelectedList));
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public string Error => null;

}

编辑:在上面添加了全 View 模型代码。

最佳答案

要使其正常工作,请确保 JCCISelectedList指定为 ObservableCollection这将解决您的问题。
否则,您需要为每个引发 Property changed 事件Description ContractAmt .... 每个元素单独作为 raiseproperty changed 事件不会为元素更改生成任何事件作为 JCCISelectedList 的属性当元素更改其正在更改的数据上下文时保持不变。

关于c# - MVVM 中的 Datagrid 绑定(bind)和编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647246/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com