M
M
murad882015-08-08 22:48:32
WPF
murad88, 2015-08-08 22:48:32

How to create an XPath query for Polyline.Points?

Help me to make an XPath query, I can't do it for a day already.
There is XML

<Root>
  <Chart>
    <Title>График 1</Title>
    <Points Color="Black">
      <Point Value="1 1 10 10"/>
      <Point Value="2 2 20 20"/>
      <Point Value="3 3"/>
    </Points>
  </Chart>

  <Chart>
    <Title>График 2</Title>
    <Points Color="Green">
      <Point Value="1 1 10 10"/>
      <Point Value="2 2 20 20"/>
      <Point Value="3 3"/>
    </Points>
  </Chart>
</Root>

You need to make a query that will return all Points and build a graph based on them.
I do it like this, but only the first Point (1 1 10 10) gets into Polyline.Points and therefore only one straight line is drawn in each graph.
<Polyline Points="{Binding XPath=//Root/Chart/Points/Point/@Value}" />

How to make sure that all Points, and not just the first one, get into the chart?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2015-08-09
@murad88

Unfortunately, it is not possible to bind an XPath query that returns several nodes to some property of a type that is not related to the XML structure of the document (for example, to a property of the string type). this will require the aggregation of this set of nodes into a single value (string), and it is not clear how to do this aggregation in the general case. Those. it is not clear how to collect ONE row from multiple Value attribute values.
There are two ways I can recommend you:
1) turn ALL the contents of the Points node into a single string. Here is a working version:

<Window x:Class="PolygonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <XmlDataProvider x:Key="ChartData">
            <x:XData>
                <Root xmlns="">
                    <Chart>
                        <Title>График 1</Title>
                        <Points Color="Black">
                            1,1 10,10
                            40,30 20,20
                            15,87
                        </Points>
                    </Chart>
                </Root>
            </x:XData>
    </XmlDataProvider>
  </Window.Resources>
    <Grid Name="grid" DataContext="{Binding Source={StaticResource ChartData}}">
        <Polygon Name="polygon" Points="{Binding XPath=/Root/Chart[1]/Points}" Stroke="#FFD33D3D" />
    </Grid>
</Window>

2) process the XML manually, bypassing WPF and any bindings, in the viewmodel, and assemble a normal PointCollection yourself, set this collection as a property and bind to it from the polygon with the usual binding
Here is a discussion of the problem on SO , it looks like your case, only easier. In the answer they write that even an attempt to apply the converter does not help, since many nodes are cut off to the very first one right away.

A
ar4ebaldello, 2015-08-09
@ar4ebaldello

Have you tried "Root/Chart/Points/ Point /@Value" with "Root/Chart/Points/@Value"?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question