Bonjour,
Je souhaite réaliser un binding sur une classe personlisée.
Le contexte :
A la création d'une fenêtre, je passe en paramètre un DataRow au constructeur.
Sur le formulaire, j'ai plusieurs TextBox bindées sur les champs du DataRow (ceci fonctionne)
Là où pour moi ça se complique, c'est que je souhaite reprendre ces informations sur un graphique en prenant en compte les modifications faites dans le TextBox.
J'ai choisi de passer par une collection dont voici le code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
Public Class ColumnDataPointCollection
Inherits ObservableCollection(Of ElementDataPoint)
End Class
Public Class ElementDataPoint
Inherits DependencyObject
Implements INotifyPropertyChanged
Public Shared ReadOnly ElementChimiqueProperty As DependencyProperty = DependencyProperty.Register("ElementChimique", GetType(String), GetType(ElementDataPoint), New PropertyMetadata(Nothing))
Public Shared ReadOnly ValeurProperty As DependencyProperty = DependencyProperty.Register("Valeur", GetType(Double), GetType(ElementDataPoint))
Public Property ElementChimique() As String
Get
Return CType(GetValue(ElementChimiqueProperty), String)
End Get
Set(value As String)
SetValue(ElementChimiqueProperty, value)
NotifyPropertyChanged("ElementChimique")
End Set
End Property
Public Property Valeur() As Double
Get
Return CType(GetValue(ValeurProperty), Double)
End Get
Set(value As Double)
SetValue(ValeurProperty, value)
NotifyPropertyChanged("Valeur")
End Set
End Property
Public Sub New()
MyBase.New()
End Sub
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class |
Je n'avais pas, au début, créé les DependencyProperty ce qui rendait impossible le Binding.
Voici le code XAML du Chart :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
<c:Chart Name="ElementsChart" Grid.Row="1" Background="{DynamicResource ResourceKey=GrayLinearGradientBrush}">
<c:Chart.Axes>
<c:CategoryAxis SortOrder="Ascending" Orientation="X" Location="Bottom" Title="Elements chimiques" ShowGridLines="False" />
<c:LinearAxis Title="Teneur en %" Orientation="Y" Location="Left" Minimum="0" />
</c:Chart.Axes>
<c:Chart.Series>
<c:ColumnSeries Name="MétalBaseSerie" Title="{Binding Path=Désignation, Mode=TwoWay}" IndependentValuePath="ElementChimique" DependentValuePath="Valeur" IsSelectionEnabled="True">
<c:ColumnSeries.ItemsSource>
<l:ColumnDataPointCollection>
<l:ElementDataPoint ElementChimique="C" Valeur="{Binding Path=C}"/>
<l:ElementDataPoint ElementChimique="Mn" Valeur="{Binding Path=Mn}"/>
<l:ElementDataPoint ElementChimique="Cr" Valeur="{Binding Path=Cr}"/>
<l:ElementDataPoint ElementChimique="Mo" Valeur="{Binding Path=Mo}"/>
</l:ColumnDataPointCollection>
</c:ColumnSeries.ItemsSource>
</c:ColumnSeries>
</c:Chart.Series>
</c:Chart> |
Si je remplace :
<l:ElementDataPoint ElementChimique="C" Valeur="{Binding Path=C}"/>
par :
<l:ElementDataPoint ElementChimique="C" Valeur="1"/>
le graphique est correctement affiché.
J'en déduit que le binding sur ma propriété "Valeur" n'est pas fait.
J'avais pensé aussi à binder directement mon DataRow comme étant la source de données du Chart mais le DataRow contient des champs que je ne veux pas mettre sur le chart.
Ma question est donc la suivante :
Comment binder certains champs du DataRow à ma ColumnSeries tout en sachant que le graph doit se mettre à jour si je modifie les champs dans les TextBox associées?
Partager