הנה כך עושים בדטה גריד שדה מחושב וגם סיכום עמודה, בבקשה תהנו:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="386" Width="525">
<Grid>
<StackPanel Margin="12,12,12,44">
<DataGrid AutoGenerateColumns="False"
Height="255" Margin="0" Name="datagrid1"
ItemsSource="{Binding}" RowDetailsVisibilityMode="Visible">
<DataGrid.Columns>
<DataGridTextColumn Width="150"
Header="Name"
Binding="{Binding Path=Name}" />
<DataGridTextColumn Width="100"
Header="Unit Price"
Binding="{Binding Path=UnitPrice, UpdateSourceTrigger=PropertyChanged,
NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, BindsDirectlyToSource=True}"/>
<DataGridTextColumn Width="100" Header="Amount"
Binding="{Binding Path=Amount, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, BindsDirectlyToSource=True}"/>
<DataGridTextColumn Width="100" Header="Total"
Binding="{Binding Path=Total, StringFormat=C}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=datagrid1, Path=RowHeaderActualWidth}"/>
<ColumnDefinition Width="{Binding ElementName=datagrid1, Path=Columns[0].ActualWidth}"/>
<ColumnDefinition Width="{Binding ElementName=datagrid1, Path=Columns[1].ActualWidth}"/>
<ColumnDefinition Width="{Binding ElementName=datagrid1, Path=Columns[2].ActualWidth}"/>
</Grid.ColumnDefinitions>
<TextBlock />
<TextBlock Grid.Column="2">Count:</TextBlock>
<TextBlock Grid.Column="3" >Avg:</TextBlock>
</Grid>
</StackPanel>
<Label Height="28" Name="totalLabel" Width="155" BorderBrush="Black" BorderThickness="2" Margin="74,311,274,8" ContentStringFormat="C" />
<Label Content="total" Height="28" Name="Label1" Margin="7,311,404,8" />
</Grid>
</Window>
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Class MainWindow
WithEvents DB As New ObservableCollection(Of Product)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
datagrid1.DataContext = DB
End Sub
Private Function SumDB() As Single
Dim total As Single = 0
For Each product As Product In DB
If product.Total IsNot Nothing Then
total += product.Total
End If
Next
Return total
End Function
Private Sub DB_CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles DB.CollectionChanged
Dim P As Product = DirectCast(e.NewItems(0), Product)
AddHandler P.PropertyChanged, AddressOf ProductPropertyChangedHandler
End Sub
Private Sub ProductPropertyChangedHandler(sender As Object, e As PropertyChangedEventArgs)
If e.PropertyName = "Total" Then
totalLabel.Content = SumDB()
End If
End Sub
End Class
Public Class Product
Implements INotifyPropertyChanged
Public Property Name As String
Private _UnitPrice As Single?
Public Property UnitPrice As Single?
Get
Return _UnitPrice
End Get
Set(value As Single?)
_UnitPrice = value
OnPropertyChanged("UnitPrice")
OnPropertyChanged("Total")
End Set
End Property
Private _Amount As Single?
Public Property Amount As Single?
Get
Return _Amount
End Get
Set(value As Single?)
_Amount = value
OnPropertyChanged("Amount")
OnPropertyChanged("Total")
End Set
End Property
Private _Total As Single?
Public ReadOnly Property Total As Single?
Get
_Total = Amount * UnitPrice
Return _Total
End Get
End Property
#Region "INotifyPropertyChanged Members"
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
#End Region
End Class
פורסם במקור בפורום CODE613 ב07/11/2013 11:15 (+02:00)