datagrid WPF中的计算列 [英] Calculated column in datagrid WPF
问题描述
这是我的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 INotifyPropertyChanged
in 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屋!