datagrid WPF中的计算列 [英] Calculated column in datagrid WPF

查看:265
本文介绍了datagrid WPF中的计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有C#的WPF中的datagrid的应用程序,我有4列,我需要相对于其他列计算四列。我使用的是IValueConverter,它完美地工作,但是当我更改行时,只计算第四列的值,当我更改单元格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 =MainWindowHeight =350Width =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
标题=总计
Binding ={Binding Converter = {StaticResource CalculatingConverter}}
/>
< /DataGrid.Columns>
< / DataGrid>

< / Grid>



这是我的代码: / p>

  public partial class MainWindow:Window 
{
public MainWindow()
{
InitializeComponent();
列表< 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 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价格
{
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文化)
{
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 ();
}
}

我该怎么做?

解决方案

最后感谢Ed Plunkett我在我的项目中实现了 INotifyPropertyChanged 类和价格计数强制调用 OnPropertyChanged (TotalPrice)并且完美地工作:

  public class Item:INotifyPropertyChanged 
{
私有字符串名称;
私人十进制价格;
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价格
{
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; }
}
}


I've a application with a datagrid in WPF with C#, I've 4 columns, and I need that the four column is calculated with respect to the other columns. I use IValueConverter, that works perfectly, but only calculates values of the column four when I change the row, and I need that when I change the cell 2 and 3, changes the 4 column.

This is my 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>

This is my code:

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

And this is my 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();
    }
}

How can I do it?

解决方案

Finally thanks to Ed Plunkett i implement INotifyPropertyChangedin my item class and in Price and Count force to call to OnPropertyChanged("TotalPrice") and works perfectly:

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

这篇关于datagrid WPF中的计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆