Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Doing simple calculations within a DataGrid row.

$
0
0

Hi,

i've got a Datagrid control which is bound to an object  of cOrdersCollection containing cOrders items. The datagrid contains 3columns. The first column, a DataGridComboBoxColumn is bound to the property Substance to select a substance which contains a Name and a EnergyContent property. Into the second column the user should enter a number of a certain dose.

Now, i want to sum the dose value of the second column and the EnergyContent value of the first column and display the result within the third column. I've no idea how to achieve this with BINDING:

<Window.Resources><CollectionViewSource x:Key="cvsOrders" Source="{Binding Source={x:Static Appllication.Current}, Path=Orders}"/><CollectionViewSource x:Key="cvsSubstances" Source="{Binding Source={x:Static Application.Current}, Path=Substances}"/></Window.Resources><DataGrid Grid.Row="2" AutoGenerateColumns="False" CanUserAddRows="True" ItemsSource="{Binding Source={StaticResource cvsOrders}}" Name="dgOrders"><DataGrid.Columns><DataGridComboBoxColumn Header="Name" ItemsSource="{Binding Source={StaticResource cvsSubstances}}" DisplayMemberPath="Name" SelectedItemBinding="{Binding Substance}" SelectedValuePath="{Binding Value}"/><DataGridTextColumn Header="Dose" Binding="{Binding Converter={StaticResource fixedDoseConverter}, Path=FixedDose}"/><DataGridTextColumn Header="TotalContent"/></DataGrid.Columns></DataGrid>

Code:

Imports System.Collections.ObjectModel

Public Class cSubstanceCollection
  Inherits ObservableCollection(Of cSubstance)

  Public Sub New()
    MyBase.New()

  End Sub
End Class

Imports System.ComponentModel
Imports System.Xml.Serialization<Serializable()>
Public Class cSubstance
  Implements INotifyPropertyChanged ', IDataErrorInfo

  Private _sName As String
  Private _sngE As Single

  Public Event PropertyChanged As PropertyChangedEventHandler _
    Implements INotifyPropertyChanged.PropertyChanged

  Public Sub New()
  End Sub

  <XmlAttribute("name")> _
  Public Property Name() As String
    Get
      Return _sName
    End Get
    Set(ByVal value As String)
      If String.IsNullOrWhiteSpace(value) Then
        Throw New ArgumentException("Name must not be empty.")
      End If

      _sName = value
      OnPropertyChanged("Name")
    End Set
  End Property<XmlElement("e")> _
  Public Property EnergyContent() As Single
    Get
      Return _sngE
    End Get
    Set(ByVal value As Single)
      If (_sngE = value) Then Return
      If value < 0 Then
        Throw New ArgumentException("EnergyContent must not be negative.")
      End If

      _sngE = value
      OnPropertyChanged("EnergyContent")
    End Set
  End Property

  Protected Sub OnPropertyChanged(ByVal name As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
  End Sub

End Class

Imports System.ComponentModel

Public Class cOrder
  Implements INotifyPropertyChanged

#Region " Fields "

  Private _substance As cSubstance
  Private _dose As Integer

  'Events
  Public Event PropertyChanged As PropertyChangedEventHandler _
    Implements INotifyPropertyChanged.PropertyChanged

#End Region

#Region " Properties "

  Public Property Substance As cSubstance
    Get
      Return _substance
    End Get
    Set(value As cSubstance)
      _substance = value
      OnPropertyChanged("Substance")
    End Set
  End Property

  Public Property Dose As Integer
    Get
      Return _dose
    End Get
    Set(value As Integer)
      _dose = value
      OnPropertyChanged("Dose")
    End Set
  End Property

#End Region

#Region " Methods "

  Protected Sub OnPropertyChanged(ByVal name As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
  End Sub

#End Region

End Class
Public Class cOrderCollection
  Inherits Collections.ObjectModel.ObservableCollection(Of cOrder)

  Public Sub New()
    MyBase.New()

  End Sub

End Class


Kind regards!


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>