Benutzer mit den meisten Antworten
Bennennung von Databindings in einer Klasse

Frage
-
Hallo zusammen,
ich habe mal eine Frage....
Und zwar habe ich mir eine Klasse geschrieben, die Daten mit Hilfe von Oxyplot darstellt:using System.ComponentModel; using OxyPlot; using OxyPlot.Axes; using OxyPlot.Series; namespace iSys { public class Signal_Plot : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private PlotModel plotModel; public PlotModel SignalPlot { get { return plotModel; } set { plotModel = value; OnPropertyChanged("PlotModel"); } } private int XMaximum { get; set; } public struct LineConfig { public string Title; public int StrokeThickness; public int MarkerSize; public OxyColor MarkerStroke; public MarkerType Marker; public bool CanTrackerInterpolatePoints; public bool Smooth; public LineConfig(string Title, int StrokeThickness, int MarkerSize, OxyColor MarkerStroke, MarkerType Marker, bool CanTrackerInterpolatePoints, bool Smooth) { this.Title = Title; this.StrokeThickness = StrokeThickness; this.MarkerSize = MarkerSize; this.MarkerStroke = MarkerStroke; this.Marker = Marker; this.CanTrackerInterpolatePoints = CanTrackerInterpolatePoints; this.Smooth = Smooth; } } /// <summary> /// Konstruktor /// </summary> public Signal_Plot() { SignalPlot = new PlotModel(); } /// <summary> /// Erzeugt einen Graphen mit einer festen X- und Y-Achse /// </summary> /// <param name="Legendtitel"></param> /// <param name="XMinimum"></param> /// <param name="XMaximum"></param> /// <param name="YMinimum"></param> /// <param name="YMaximum"></param> public void SetUpStaticModel(string Legendtitel, int XMinimum, int XMaximum, int YMinimum, int YMaximum) { SignalPlot.LegendTitle = Legendtitel; SignalPlot.LegendOrientation = LegendOrientation.Horizontal; SignalPlot.LegendPlacement = LegendPlacement.Outside; SignalPlot.LegendPosition = LegendPosition.TopRight; SignalPlot.LegendBackground = OxyColor.FromAColor(200, OxyColors.White); SignalPlot.LegendBorder = OxyColors.Black; this.AddAxis(AxisPosition.Left, YMinimum, YMaximum); this.AddAxis(AxisPosition.Bottom, XMinimum, XMaximum); this.XMaximum = XMaximum; } /// <summary> /// /// </summary> /// <param name="Legendtitel"></param> /// <param name="YMinimum"></param> /// <param name="YMaximum"></param> public void SetUpDynamicModel(string Legendtitel, int XMinimum, int XMaximum) { SignalPlot.LegendTitle = Legendtitel; SignalPlot.LegendOrientation = LegendOrientation.Horizontal; SignalPlot.LegendPlacement = LegendPlacement.Outside; SignalPlot.LegendPosition = LegendPosition.TopRight; SignalPlot.LegendBackground = OxyColor.FromAColor(200, OxyColors.White); SignalPlot.LegendBorder = OxyColors.Black; for (int i = XMinimum; i < XMaximum; i++) { SignalPlot.Series.Add(new LineSeries { LineStyle = LineStyle.Solid }); } } /// <summary> /// Fügt dem Plot eine Achse hinzu /// </summary> /// <param name="Position">Achsenposition</param> /// <param name="Minimum">Kleinster Wert</param> /// <param name="Maximum">Größter Wert</param> public void AddAxis(AxisPosition Position, int Minimum, int Maximum) { SignalPlot.Axes.Add(new LinearAxis { Position = Position, Minimum = Minimum, Maximum = Maximum }); } /// <summary> /// /// </summary> /// <param name="Config"></param> public void AddSeries(LineConfig Config) { var Graph = new LineSeries { Title = Config.Title, StrokeThickness = Config.StrokeThickness, MarkerSize = Config.MarkerSize, MarkerStroke = Config.MarkerStroke, MarkerType = Config.Marker, CanTrackerInterpolatePoints = Config.CanTrackerInterpolatePoints, Smooth = Config.Smooth, }; SignalPlot.Series.Add(Graph); } /// <summary> /// Fügt einem ausgewählten Plot einen Datenpunkt hinzu /// </summary> /// <param name="Plot">Plot, dem der Datenpunkt hinzugefügt werden soll</param> /// <param name="Data">Neuer Datenpunkt</param> public void AddDataPoint(int Plot, DataPoint Data) { lock (SignalPlot.SyncRoot) { var Graph = (LineSeries)SignalPlot.Series[Plot]; if (Graph != null) { if (Graph.Points.Count >= XMaximum) { Graph.Points.RemoveAt(0); } Graph.Points.Add(Data); } } } /// <summary> /// Zeichnet den Graphen neu /// </summary> public void RefreshPlot() { lock (SignalPlot.SyncRoot) { SignalPlot.InvalidatePlot(true); } } protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } } }
Jetzt besteht das "Problem", dass diese Klasse nur für einen Plot funktioniert, der das Databinding "SignalPlot" besitzt:
<oxy:PlotView x:Name="Signal_Plot" Model="{Binding SignalPlot}" Margin="263,6,12,359"/>
Jetzt würde ich die Klasse natürlich eher allgemein benutzen wollen, sprich die Benennung des Databindings variable machen, sodass ich in meinem XAML-Code z.B. vier Plots a, b, c und d habe, die ein entsprechendes, aber unterschiedliches, Databinding besitzen, die Klasse aber für alle vier Plots funktioniert, wo ich z.B. im Konstruktor den Namen des Bindings im XAML angebe.
Gibt es da eine Möglichkeit das zu realisieren?
Antworten
-
Hi,
im Binding gibst Du nicht den Namen eines DataBindings an, sondern die Eigenschaft, die ein Datenmodell bereitstellt. Um in unterschiedlichen PlotViews unterschiedliche Datenmodelle zu nutzen, kann man ein angehängtes Verhalten (attached behavior) nutzen, welchem man einen Parameter übergibt, der dann in der SignaPlot-Eigenschaft das entsprechende Datenmodell bereitstellt.--
Viele Grüsse
Peter Fleischer (MVP, Partner)
Meine Homepage mit Tipps und Tricks- Als Antwort vorgeschlagen Dimitar DenkovMicrosoft contingent staff, Administrator Freitag, 4. September 2015 09:04
- Als Antwort markiert Aleksander Chalabashiev Montag, 7. September 2015 08:05