1

I work on that problem from one day, I use a XCeed chart in WPF,like this :

    <xceedTk:Chart Name="graphComptage" VerticalAlignment="Top"  MinWidth="400"  MinHeight="400" Height="Auto" Width="Auto" IsEnabled="True" 
                         BorderThickness="0" Background="Transparent" BorderBrush="Transparent" DataContext="{Binding}"
                         Visibility="{Binding AfficherErreur, Converter={conv:BoolConverter TrueValue=Collapsed, FalseValue=Visible}}">
                <xceedTk:Chart.Legend>
                    <xceedTk:Legend Title="{Binding TitreLegende}" Width="{Binding LargeurLegende}" Height="Auto" BorderThickness="0" />
                </xceedTk:Chart.Legend>
                <xceedTk:Chart.Areas>
                    <xceedTk:Area Title="{Binding TitreLegende}">
                        <xceedTk:Axis Orientation="Vertical" Margin="0 0 5 0" ShowGridLines="True" GraduationMode="Automatic" LabelsType="DateTime"  />
                        <xceedTk:Axis Orientation="Horizontal" Margin="0 0 5 0" ShowGridLines="True" GraduationMode="Automatic" LabelsType="Numeric" />
                        <xceedTk:Area.Series>
                            <xceedTk:Series DataPointsSource="{Binding DataContext.SeriesPoints, ElementName=ucVueTabulaireGraphique}">
                                <xceedTk:Series.Layout>
                                    <xceedTk:LineLayout />
                                </xceedTk:Series.Layout>
                                <xceedTk:Series.DataPointBindings>
                                    <xceedTk:BindingInfo PropertyName="Y">
                                        <xceedTk:BindingInfo.Binding>
                                            <Binding Path="Value"/>
                                        </xceedTk:BindingInfo.Binding>
                                    </xceedTk:BindingInfo>
                                    <xceedTk:BindingInfo PropertyName="X">
                                        <xceedTk:BindingInfo.Binding>
                                            <Binding Path="Key"/>
                                        </xceedTk:BindingInfo.Binding>
                                    </xceedTk:BindingInfo>
                                </xceedTk:Series.DataPointBindings>
                            </xceedTk:Series>
                        </xceedTk:Area.Series>
                    </xceedTk:Area>
                </xceedTk:Chart.Areas>
            </xceedTk:Chart>

(All the XAML file is here)

I want to bind this on a dictionary in my ViewModel, when I check in the output window, I have in all time a similar message :

    System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.SeriesPoints; DataItem=null; target element is 'Series' (HashCode=49725882); target property is 'DataPointsSource' (type 'IEnumerable')

It seems that I cannot access at the public property in the view Model, I tried various manner to bind to my ViewModel properties, (FindAncestor, directly), but I always have the same result.

Does somebody stil have an idea?

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
Frip
  • 129
  • 1
  • 8
  • What is ucVueTabulaireGraphique and where is your view model? – mm8 Nov 06 '17 at 15:27
  • ucVueTabulaireGraphique is a UsersControl and my ViewModel is bind to the DataContext of this UserControl. In my ViewModel I have this properties: public List SeriesPoints { get => _seriesPoints; set { _seriesPoints = value; NotifyPropertyChanged(); } } – Frip Nov 06 '17 at 15:31
  • Is the Chart a child of the UserControl...? – mm8 Nov 06 '17 at 15:32
  • No there is a lot of thing before. This user is in a tabcontrol too. – Frip Nov 06 '17 at 15:34
  • Unless you think someone will be able to guess how your XAML is structured, you should post it if you want any help. – mm8 Nov 06 '17 at 15:35
  • I put the XAML file on link. – Frip Nov 06 '17 at 16:50
  • Is this for the **paid** version of the toolkit? – jsanalytics Nov 06 '17 at 19:09
  • Eventually, yes, but for the moment, I don't have a good experience, I was with DataVisualizationToolKit before, very much easier to implements, and for me the look and feel was better, but it have a memory leak, we need to change the control. – Frip Nov 06 '17 at 20:35
  • Ok... I'm asking because I wanted to create a specific tag for the paid versions, so I'm looking for potential users...:O) otherwise the tag gets dropped/refused. – jsanalytics Nov 06 '17 at 20:43
  • So, I want to tag your question with `xceed-plus-edition` (new tag), if that's ok with you. This is justifiable since the XAML for this edition is different from the community version. – jsanalytics Nov 06 '17 at 20:55
  • It is perfect for me, there is not a lot of documentation for the paid edition, and I have a lot a difficulties to get information. The case i want to do is so simple, it just a graph with a (1...n) series, (linear, graphic), For the moment, I have a lot a difficulties with the binding but I am looking for example in the project sample and I don't see anything representing something similar to the final result I need in my application. – Frip Nov 06 '17 at 21:01
  • I'm looking into your binding problem. I get the very same binding error you did in a much simpler scenario. If you google that error, **Cannot find governing FrameworkElement** you will find out that one solution is _stealing the DataContext_... which quite frankly sucks...:O) The reason for this, from what I read, is that sometimes some parts of the control is not in the visual tree, causing the problem... – jsanalytics Nov 06 '17 at 21:06
  • Take a look at [this explanation](https://stackoverflow.com/questions/7660967/wpf-error-cannot-find-governing-frameworkelement-for-target-element). – jsanalytics Nov 06 '17 at 21:08
  • Yes I tried that solution too, I think I will stop wasting my time with that control and try another one. – Frip Nov 06 '17 at 21:09

1 Answers1

2

The fact that you're using a Dictionary alone could be the problem because it does not implement the necessary notification interfaces required for proper binding. In general, use an ObservableCollection instead. In particular, Xceed components, based on their own sample code, seem to favor the usage of CollectionViewSource. So, here's some working sample code for you:

See resulting chart image here.

XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 
        x:Class="WpfApp1.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <CollectionViewSource x:Key="cvs1" Source="{Binding Data}"/>
    </Window.Resources>

    <Window.DataContext>
        <local:MyViewModel/>
    </Window.DataContext>

    <Grid>
        <xctk:Chart>
            <xctk:Chart.Areas>
                <xctk:Area>
                    <xctk:Area.Series>
                        <xctk:Series DataPointsSource="{Binding Source={StaticResource cvs1}}">
                            <xctk:Series.Layout>
                                <xctk:LineLayout />
                            </xctk:Series.Layout>
                            <xctk:Series.DataPointBindings>
                                <xctk:BindingInfo PropertyName="X">
                                    <xctk:BindingInfo.Binding>
                                        <Binding Path="Item1"/>
                                    </xctk:BindingInfo.Binding>
                                </xctk:BindingInfo>
                                <xctk:BindingInfo PropertyName="Y">
                                    <xctk:BindingInfo.Binding>
                                        <Binding Path="Item2"/>
                                    </xctk:BindingInfo.Binding>
                                </xctk:BindingInfo>
                            </xctk:Series.DataPointBindings>
                        </xctk:Series>
                    </xctk:Area.Series>
                </xctk:Area>
            </xctk:Chart.Areas>
        </xctk:Chart>
    </Grid>
</Window>

View Model:

public class MyViewModel
{
    public ObservableCollection<Tuple<int,int>> Data { get; set; }

    public MyViewModel()
    {
        Data = new ObservableCollection<Tuple<int, int>>();

        Random y = new Random();

        for (int x = 0; x < 15; x++)
            Data.Add(new Tuple<int, int>(x, y.Next(100)));

    }
}

Note that, although it uses a StaticResource in the binding statement, if you dynamically updated the underlying Data property in the view model, the chart will dynamically update as well.