0

I'm doing a custom PieChart with the Winrt Xaml Toolkit and I'm having difficulty binding to a property of my main element. Here's the code:

<Charting:Chart x:Name="PieChart" Title="Pie Title" Width="300" Height="300">
    <Charting:Chart.Series>
        <Charting:PieSeries Title="Population" IndependentValueBinding="{Binding Name}" DependentValueBinding="{Binding Amount}" IsSelectionEnabled="False" Width="125" Height="125" />
    </Charting:Chart.Series>
    <Charting:Chart.LegendStyle>
        <Style TargetType="datavis:Legend">
            <Setter Property="ItemContainerStyle" xmlns:series="using:WinRTXamlToolkit.Controls.DataVisualization.Charting">
                <Setter.Value>
                    <Style TargetType="series:LegendItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="series:LegendItem">
                                    <StackPanel Orientation="Horizontal">
                                        <datavis:Title Content="{TemplateBinding Content}" />
                                        <TextBlock Text="{Binding Amount}" />
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    </Charting:Chart.LegendStyle>
</Charting:Chart>

In my LegendItem I have a TextBlock to which I want to bind the DependentValueBinding property from my Charting:PieSeries element. How can I do this? I also tried referencing the element Self but cannot get the sub properties.

Schrödinger's Box
  • 3,218
  • 1
  • 30
  • 51

2 Answers2

0

Did you try to set the Name of your PieSeries and bind by ElementName?

Sample fragment of your code below:

  <Charting:Chart.Series>
    <Charting:PieSeries x:Name="MySeries" Title="Population" IndependentValueBinding="{Binding Name}" DependentValueBinding="{Binding Amount}" IsSelectionEnabled="False" Width="125" Height="125" />
  </Charting:Chart.Series>


  <ControlTemplate TargetType="series:LegendItem">
    <StackPanel Orientation="Horizontal">
        <datavis:Title Content="{TemplateBinding Content}" />
        <TextBlock Text="{Binding DependentValueBinding, ElementName=MySeries}" />
    </StackPanel>
  </ControlTemplate>
marcinax
  • 1,067
  • 7
  • 10
  • I think it's almost there, it prints me a string with `Windows.UI.Xaml.Data.Binding`. I'm trying several properties of that element, but so far I only managed to get the Binding name, i.e., `Amount.` – Schrödinger's Box Oct 05 '15 at 19:49
  • Sorry, DependentValueBinding is type of Binding so you can't do this by ElementName. Can you show me how do you set ItemsSource or add items to your pie series? Does your chart show series? A screenshot of your current progress will be useful. – marcinax Oct 05 '15 at 20:52
  • You can take a look at what you asked here: [http://pasteboard.co/19QoBKn7.png](http://pasteboard.co/19QoBKn7.png) – Schrödinger's Box Oct 05 '15 at 21:34
0

If you debug the visual tree of a PieSeries - you'll see that the DataContext of a LegendItem is a PieDataPoint and not your item, so it won't have an Amount property. The Title element also has PieDataPoint as the DataContext, but the Content="{TemplateBinding Content}" part makes the DataContext of its ContentPresenter be your item. Perhaps then you could make it work by doing this:

<StackPanel Orientation="Horizontal">
    <datavis:Title Content="{TemplateBinding Content}" />
    <TextBlock DataContext="{TemplateBinding Content}" Text="{Binding Amount}" />
</StackPanel>
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100