C# datagrid WPF中的计算列
我有一个应用程序,在WPF中有一个数据网格,带有C#,我有4列,我需要这4列是相对于其他列计算的。我使用了IValueConverter,它工作得很好,但是当我更改行时,只计算第四列的值,当我更改单元格2和单元格3时,我需要它,更改第四列 这是我的XAML:C# datagrid WPF中的计算列,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个应用程序,在WPF中有一个数据网格,带有C#,我有4列,我需要这4列是相对于其他列计算的。我使用了IValueConverter,它工作得很好,但是当我更改行时,只计算第四列的值,当我更改单元格2和单元格3时,我需要它,更改第四列 这是我的XAML: <Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<Window x:Class="WpfApplication6.MainWindow"
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:WpfApplication6"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="grid">
<DataGrid.Resources>
<local:CalculatingConverter x:Key="CalculatingConverter" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridTextColumn Binding="{Binding Count}"/>
<DataGridTextColumn Binding="{Binding Price}" />
<DataGridTextColumn
Header="Total"
Binding="{Binding Converter={StaticResource CalculatingConverter}}"
/>
</DataGrid.Columns>
</DataGrid>
</Grid>
我该怎么做呢?您必须使用
IValueConverter
进行此操作。使用第一列作为源(将第二列绑定到第一列),并使用转换器转换值
.为此,您必须使用
IValueConverter
。使用第一列作为源(将第二列绑定到第一列),并使用转换器转换值
.您的
项
类需要实现INotifyPropertyChanged
并在其任何属性更改时引发PropertyChanged
事件
问题是绑定无法知道需要监视哪些属性以进行更改。一个解决方案是将转换器更改为IMultiValueConverter
,并将该绑定更改为一个MultiBinding
,它显式地绑定Price
和Count
您的另一个选项(这是一个简单得多的选项)是将只读的TotalPrice
属性添加到项目
类中。这就是我要做的
public decimal TotalPrice {
get { return Price * Count; }
}
然后,在Price
属性中,当Price
更改时,您将为TotalPrice
以及Price
引发PropertyChanged
事件。对于Count
,您也可以这样做
然后您可以在XAML中非常简单地绑定到TotalPrice
,而无需任何转换器。您可能需要使绑定Mode=one-way
,因为它是只读的
如果您需要更多的代码示例,请告诉我 您的
项
类需要实现INotifyPropertyChanged
并在其任何属性更改时引发PropertyChanged
事件
问题是绑定无法知道需要监视哪些属性以进行更改。一个解决方案是将转换器更改为IMultiValueConverter
,并将该绑定更改为一个MultiBinding
,它显式地绑定Price
和Count
您的另一个选项(这是一个简单得多的选项)是将只读的TotalPrice
属性添加到项目
类中。这就是我要做的
public decimal TotalPrice {
get { return Price * Count; }
}
然后,在Price
属性中,当Price
更改时,您将为TotalPrice
以及Price
引发PropertyChanged
事件。对于Count
,您也可以这样做
然后您可以在XAML中非常简单地绑定到TotalPrice
,而无需任何转换器。您可能需要使绑定Mode=one-way
,因为它是只读的
如果您需要更多的代码示例,请告诉我 最后感谢Ed Plunkett,我在我的
项目类和Price
和Count
中实现了INotifyPropertyChanged
,并强制调用OnPropertyChanged(“TotalPrice”)
,效果非常好:
public class Item : INotifyPropertyChanged
{
private string name;
private decimal price;
private int count;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Name {
get
{
return this.name;
}
set
{
if (value != name)
{
name = value;
OnPropertyChanged();
}
}
}
public decimal Price
{
get
{
return price;
}
set
{
if (value != this.Price)
{
price = value;
OnPropertyChanged();
OnPropertyChanged("TotalPrice");
}
}
}
public int Count {
get
{
return count;
}
set
{
if (value != count)
{
count = value;
OnPropertyChanged();
OnPropertyChanged("TotalPrice");
}
}
}
public decimal TotalPrice
{
get { return Price * Count; }
}
}
最后,感谢Ed Plunkett,我在我的项目
类和价格
和计数
中实现了INotifyPropertyChanged
,并强制调用OnPropertyChanged(“TotalPrice”)
,效果非常好:
public class Item : INotifyPropertyChanged
{
private string name;
private decimal price;
private int count;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Name {
get
{
return this.name;
}
set
{
if (value != name)
{
name = value;
OnPropertyChanged();
}
}
}
public decimal Price
{
get
{
return price;
}
set
{
if (value != this.Price)
{
price = value;
OnPropertyChanged();
OnPropertyChanged("TotalPrice");
}
}
}
public int Count {
get
{
return count;
}
set
{
if (value != count)
{
count = value;
OnPropertyChanged();
OnPropertyChanged("TotalPrice");
}
}
}
public decimal TotalPrice
{
get { return Price * Count; }
}
}
您知道这些列中的值来自哪里吗?你能澄清一下“立即执行或更改第一列”是什么意思吗?我需要知道,当我在第一列中更改值时,第二列也会更改值。你知道值来自哪里吗?您是如何将它们放入网格的?在上面的代码中,源代码是如何放置的?您知道这些列中的值来自何处吗?你能澄清一下“立即执行或更改第一列”是什么意思吗?我需要知道,当我在第一列中更改值时,第二列也会更改值。你知道值来自哪里吗?你是如何把它们放在网格中的?在上面的代码中是放置源代码的