syncfusion wpf chart array of double - how to use with mvvm - wpf

I have a Model with an array[100] of double and I would like to use this directly as the data for the chart. The index value should be the X and the value at that index should be the Y on the chart.
Currently I have to convert the array to a list of points (in my ViewModel) where I store the index value (0-99) as the X of the point and the value of that array index as the Y value of the point.
My xaml looks like this :
<UserControl.DataContext>
<local:ViewModel/>
</UserControl.DataContext>
<Grid>
<syncfusion:SfChart>
<chart:SfChart.PrimaryAxis>
<chart:NumericalAxis/>
</chart:SfChart.PrimaryAxis>
<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis/>
</chart:SfChart.SecondaryAxis>
<chart:LineSeries ItemsSource="{Binding Data}" XBindingPath="X" YBindingPath="Y"/>
</syncfusion:SfChart>
</Grid>
</UserControl>
I cannot imagine that this is the best way to get the result ...

The index value should be the X and the value at that index should be the Y on the chart.
Well, the only way to let the control know this is to create an object with a property that returns the X value and another that returns the Y value and then set the XBindingPath and YBindingPath to the names of these respectively. This is MVVM.
The control cannot really be supposed to figure out that you want the index to represent the Y value.

Related

wpf When binding to array index, fallbackvalue doesn't seem to work

I have a simple Grid in which i bind the Row height to an array member. But the content may be cleared in case of wrong entries from the user, that the array index becomes invalid.
Therefore, I set the FallbackValue to a default value. But this doesn't seem to work.
In the binding, I use a converter which converts a custom type to the required Double. But that should not be the source of the problem.
<RowDefinition Name="Row1" Height="{Binding Path=item[0].value, Converter={conv:ItemValueToRowHeight}, FallbackValue=20}"/>
The debug output shows me that the wpf binding system still wants the array item, even if there are no items:
System.Windows.Data Warning: 17 : Cannot get 'Item[]' value ...
Any ideas ?
Remarks
A binding returns a value successfully if:
The path to the binding source resolves successfully.
The value converter, if any, is able to convert the resulting value.
The resulting value is valid for the binding target (target) property.
http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.fallbackvalue.aspx
Make your converter return DependencyProperty.UnsetValue

Binding to a immutable field of an observable property

I'm in an interesting situation where I have geometry being painted onto a canvas. In this situation I have a PatternVisual created by DataTemplate. The PatternVisual has a DataContext of Pattern. Pattern has a property, Position. Position is of type Point2d which is an immutable struct containing two properties, X and Y.
What I'm trying to do is set the Canvas.Top and Canvas.Left attached properties on my PatternVisual specified in the DataTemplate to {Binding Position.Y} and {Binding Position.X} and have them trigger updates when Position it's self changes.
The only way I've found so far to do this is using a converter to "look up" X and Y, which seems wrong to me.

Binding a collection to WPF Toolkit chart

Here are my goals:
Using the WPF Toolkit, create a simple column chart with a linear Y-axis and a DateTime range X-axis.
Bind a collection of objects to the chart. Each object has a DateTime(X-axis data-point) and Int32(Y-axis data-point) properties.
Below is my current XAML. The XAML below has the axes I want however, the chart will not render any data:
<chartingToolkit:Chart Grid.Column="1" Grid.Row="1" Grid.RowSpan="2" Name="ColumnChart" Title="Records Loaded By Date"
VerticalAlignment="Top" Height="262">
<chartingToolkit:Chart.Axes>
<chartingToolkit:DateTimeAxis Interval="1" IntervalType="Days" x:Name="myDateTimeAxis"
Orientation="X" Title="Date">
<chartingToolkit:DateTimeAxis.AxisLabelStyle>
<Style TargetType="chartingToolkit:DateTimeAxisLabel">
<Setter Property="StringFormat" Value="{}{0:MM/dd}"/>
</Style>
</chartingToolkit:DateTimeAxis.AxisLabelStyle>
</chartingToolkit:DateTimeAxis>
<chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="True" x:Name="myYAxis"
Title="Transactions Loaded"/>
</chartingToolkit:Chart.Axes>
<chartingToolkit:Chart.Series>
<chartingToolkit:ColumnSeries DependentValuePath="TransactionLoadCount"
IndependentValuePath="Date" ItemsSource="{Binding Path=LoadStats}"
IsSelectionEnabled="True">
</chartingToolkit:ColumnSeries>
</chartingToolkit:Chart.Series>
</chartingToolkit:Chart>
Note: When I remove the XML section <chartingToolkit:Chart.Axes>...</chartingToolkit:Chart.Axes> the data WILL APPEAR but not in a format I like.
Why doesn't the chart render my collection data?
Since you figured it out and posted it in comments, I will expand on other things to look for when this problem would occur. Note to readers that came here, the problem was that the list bound to the control needs to contain > 1 DateTime in the collection in order to display a chart. This is a bug in the WPF Toolkit library. This may be fixed in the current/future versions of WPF Toolkit.
If that didn't fix your problem:
Watch your output window for any binding expressions errors when the control is load. If the data context of the chart is set right, binding the items source to an unavailable collection will cause problems.
Make sure the DateTime values that are to be used on the DateTime axis have values that span across at least 2 days, or whatever your interval is set for. If it applies, try setting a Maximum or Minimum value.
Use the process of elimination to determine if its the X or Y axis causing the issue. Comment out the X axis XAML and see if the control displays the Y axis correctly. If so, comment out the Y axis and uncomment the X axis XAML and see if it displays correctly. If not you know your problem is with one of the axes (that wasn't commented out).
Check that your StringFormat is correct. Here I believe you could just use `Value="MM/dd"
Make sure you spelled your properties for IndependentValuePath and DependentValuePath correctly.
I found these to be the biggest issues during charting experiences so this is a checklist to follow when debugging issues. Hopefully this will answer many general questions. If it doesn't suffice as an answer, you should answer it yourself OP.

Bind a Line position to the position of an element in a usercontrol

I have a canvas which contains specific usercontrols.
At some point, I want to add a line to the canvas (in code) where one of the points is bound to the position of an element in a usercontrol. The usercontrol is dynamic, so elements can change position.
The usercontrol contains a layout with grids, stackpanels etc. that the element can be located in.
For the usercontrol, I'd get the Canvas.Left and Canvas.Top property,
but since I don't want to bind to the usercontrol but to an element it contains,
how would I do that ?
(In a way that if the usercontrol's layout changes, the new position of the element is taken into account)
Let's stipulate that there are 3 things here:
"Line" is being bound to
"Child" who is contained by
"Parent".
Line should set its Canvas.Top and Canvas.Left to a Binding on Child, using a ValueConverter who needs to have access to Parent.
The ValueConverter will return
Parent.Left + Child.GetPosition(Parent).Left
(Or whatever the exact lookup methods are). The important thing is that the Child gets its position relative to the Parent, and adds in the Parent's position.
(Elaborating post comment)
So you specify a line this way:
<Line X1="50" Y1="50" X2="200" Y2="200" />
Which means that you apply a binding to each of those coordinate specifiers. Probably your best bet is to pass the X or Y to the ValueConverter as a ConverterParameter, to save you writing too much duplicate code.
Your original question specifies that you are looking for X1,Y1. I don't know what you want the other end of the line to be, it sounds like it's fixed somewhere. Or if it's going to another element you do basically the same trick there - this is going to work for one point only. Let's assume for the moment that you always want the other point to be 100,100.
So your line is specified like this:
<Line X1="{Binding ElementName=yourTarget, ConverterParameter=X, Converter={StaticResource targetLocationExtractor}}" Y1="{Binding ElementName=yourTarget, ConverterParameter=Y, Converter={StaticResource targetLocationExtractor}}" X2="200" Y2="200" />

WPF XPath Binding With Multiple Results Converted to Comma-Delimited String

I have an XML file similar in structure to the following:
<Parent>
<Child>5</Child>
<Child>3</Child>
<Child>5</Child>
<Child>1</Child>
</Parent>
In my XAML, I have a ListView bound to the XML file and have set the DataTemplate of a ListViewItem to be bound as follows:
<TextBlock Text="{Binding XPath=Parent/Child}"/>
Obviously, I'm expecting 4 results for this XPath query, but I can't seem to find a way to convert the results to a comma-delimited string, and right now, the TextBlock is just displaying the first value.
If I use the same XPath query for setting the ItemsSource of a ListBox, I get all the results in the ListBox, so I think I should be able to get all the values passed to a Converter class...
There's no way to have an XPath query that returns multiple nodes, such as yours, aggregate them into a single value for you. What's happening is that a nodeset is being returned and, since you're binding to a single string property, the infrastructure is simply coercing that nodeset by grabbing the first node from the set and then grabbing its #text node.
Honestly I have not tried this myself and don't have time at the moment, but the only way I'd expect this to ever work is if you wrote a custom IValueConverter. I assume that it will hand an XmlNodeList as the value to be converted and then you can enumerate those nodes and concatenate a comma separated string yourself.
Update
Since the IValueConverter suggestion did not work due to the XPath engine doing a pre-coercion, here's what I suggest you do: instead of binding to a single TextBlock, bind to an ItemsControl instead and define the ItemTemplate for the ItemsControl to be as follows:
<DataTemplate>
<TextBlock Text="{Binding}"/>,
</DataTemplate>
Note: in all honesty I'm taking the lazy approach in the DataTemplate and you will end up with a comma even after the last item right now. That said you should be able to define a DataTemplate with a trigger that determines that it's the last item and doesn't show the comma.
Finally, depending on how you want the data to layout also set the ItemsControl's ItemsPanel. I'm assuming you want horizontal flow with wrapping here:
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
If you want to set the XML data as source to your ListView you can do like this:
MyListView.ItemsSource = XElement.Load(#"XMLFile1.xml").Elements("Child");
and you bind to the Value property in the TextBlock:
<TextBlock Text="{Binding Path=Value}" />
If you need to modify the query you can extract the content of your XML file to a var that you can use as ItemsSource on your ListView.
XElement xmlData = XElement.Load(#"XMLFile1.xml");
var query = from x in xmlData.Elements("Child")
select x;

Resources