Skip to main content
 首页 » 编程设计

c#之数据网格 WPF 中的计算列

2024年02月05日13softidea

我有一个带有 C# 的 WPF 数据网格的应用程序,我有 4 列,我需要相对于其他列计算这四列。我使用 IValueConverter,它工作得很好,但只在我更改行时计算第 4 列的值,我需要在更改单元格 2 和 3 时更改第 4 列。

这是我的 XAML:

<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> 

这是我的代码:

    public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
        InitializeComponent(); 
        List<Item> items = new List<Item>(); 
 
        items.Add(new Item() { Name = "Item1", Count = 2, Price = 10 }); 
        items.Add(new Item() { Name = "Item2", Count = 5, Price = 20 }); 
        this.grid.ItemsSource = items; 
    } 
} 
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(); 
            } 
        } 
    } 
    public int Count { 
        get 
        { 
            return count; 
        } 
 
        set 
        { 
            if (value != count) 
            { 
                count = value; 
                OnPropertyChanged(); 
            } 
        } 
    } 
    public decimal TotalPrice 
    { 
        get { return Price * Count; } 
    } 
} 

这是我的 IConverter:

    public class CalculatingConverter : IValueConverter 
{ 
 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        try 
        { 
 
            return ((Item)value).Price * ((Item)value).Count; 
        } 
        catch 
        { 
            return null; 
        } 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
} 

我该怎么做?

请您参考如下方法:

您的 Item 类需要实现 INotifyPropertyChanged 并在其任何属性更改时引发 PropertyChanged 事件。

问题在于,该绑定(bind)无法知道它需要监视哪些属性以进行更改。一种解决方案是将您的转换器更改为 IMultiValueConverter,并将该绑定(bind)更改为 MultiBinding,它显式绑定(bind)了 Price 和 Count。

您的另一个选择是向您的 Item 类添加一个只读的 TotalPrice 属性,这将是一个更简单的选择。那就是我会做的。

public decimal TotalPrice { 
    get { return Price * Count; } 
} 

然后,在您的 Price 属性中,当 Price 更改时,您将为 TotalPrice 引发 PropertyChanged 事件以及 Price。您会对 Count 执行相同的操作。

然后您可以在 XAML 中非常简单地绑定(bind)到 TotalPrice,根本不需要转换器。您可能需要进行绑定(bind) Mode=OneWay,因为它是只读的。

如果您需要更多代码示例,请告诉我。