gpt4 book ai didi

c# - WPF DataGrid列总MVVM模式

转载 作者:行者123 更新时间:2023-12-03 10:27:57 25 4
gpt4 key购买 nike

我的DataGrid中有一列是Price字段。
在表单底部的TextBlock中。

如何根据“价格”列的值在TextBlock中显示总值?

XAML代码:

 <Grid>
<DataGrid HorizontalAlignment="Left" ItemsSource="{Binding Path=SaleryDetailsCollection, Mode=TwoWay}" AutoGenerateColumns="False" VerticalAlignment="Top" Width="640" Height="192" >
<DataGrid.Columns>
<DataGridTextColumn Header="Type" Binding="{Binding Type, Mode=TwoWay, NotifyOnTargetUpdated=True, ValidatesOnDataErrors=True}" Width="*" />
<DataGridTextColumn Header="Amount" Binding="{Binding Price, Mode=TwoWay, NotifyOnTargetUpdated=True, ValidatesOnDataErrors=True}" Width="*" />
</DataGrid.Columns>
</DataGrid>
<TextBlock HorizontalAlignment="Left" Margin="415,358,0,0" TextWrapping="Wrap" Text="{Binding SalaryTotal}" VerticalAlignment="Top"/>
</Grid>

ViewModel:
public ObservableCollection<SaleryDetailsModel> SaleryDetailsCollection
{
get { return _SaleryDetailsCollection; }
set
{
SalaryTotal = SaleryDetailsCollection.Sum(x => x.Amount);
_SaleryDetailsCollection = value;
NotifyPropertyChanged("SaleryDetailsCollection");
}
}
public Double SalaryTotal
{
get { return _SalaryTotal; }
set
{
_SalaryTotal = value;
NotifyPropertyChanged("SalaryTotal");
}
}

类SaleryDetailsMode
  class SaleryDetailsModel:ViewModel
{
private Double _Amount;
private String _Type;
public Double Amount
{
get { return _Amount; }
set
{
_Amount = value;
NotifyPropertyChanged("Amount");
}
}
public String Type { get { return _Type; } set { _Type = value; NotifyPropertyChanged("Type"); } }

}

类ViewModel
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

最佳答案

将此代码添加到构造函数中

SaleryDetailsCollection = new ObservableCollection<SaleryDetailsModel>();
SaleryDetailsCollection.CollectionChanged += MyItemsSource_CollectionChanged;

在ViewModel中
void MyItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
foreach (SaleryDetailsModel item in e.NewItems)
item.PropertyChanged += MyType_PropertyChanged;

if (e.OldItems != null)
foreach (SaleryDetailsModel item in e.OldItems)
item.PropertyChanged -= MyType_PropertyChanged;
}

void MyType_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Amount")
DoWork();
}

private void DoWork()
{
SalaryTotal = SaleryDetailsCollection.Sum(x => x.Amount);
}

XAML
<Grid>
<DataGrid HorizontalAlignment="Left" ItemsSource="{Binding Path=SaleryDetailsCollection, Mode=TwoWay}" AutoGenerateColumns="False" VerticalAlignment="Top" Width="640" Height="192" >
<DataGrid.Columns>
<DataGridTextColumn Header="Type" Binding="{Binding Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Width="*" />
<DataGridTextColumn Header="Amount" Binding="{Binding Price, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Width="*" />
</DataGrid.Columns>
</DataGrid>
<TextBlock HorizontalAlignment="Left" Margin="415,358,0,0" TextWrapping="Wrap" Text="{Binding SalaryTotal}" VerticalAlignment="Top"/>

关于c# - WPF DataGrid列总MVVM模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28658800/

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