How to place winform chart control in WPF view?

I am trying to use winforms charts which have a lot of chart controls, but I am unable to use those controls in the wpf view. I have axml code:

<Window x:Class="WPFCharts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:charts="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
        Title="MainWindow" Height="440.789" Width="863.158" Loaded="Window_Loaded">
    <Grid Name="grdgraficos">



        <TabControl VerticalAlignment="Stretch" Margin="0,0,0,0" HorizontalAlignment="Stretch">
            <TabItem Header="WPF Controls">
                <Grid Background="#FFE5E5E5">
                    <charts:Chart  Name="pieChart" Title="Pie Series Demo" Margin="0,0,0,0">
                        <charts:PieSeries DependentValuePath="y" 
                IndependentValuePath="campo" ItemsSource="{Binding}" 
                IsSelectionEnabled="True" />
                    </charts:Chart>
                </Grid>
            </TabItem>
            <TabItem Header="WinForms Controls">
                <Grid Background="#FFE5E5E5">
                    <WindowsFormsHost Name="wfhGraficoWinForm" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Bottom" />
                </Grid>
            </TabItem>
        </TabControl>

    </Grid>
</Window>

      

And I have this code. By the time this is the first solution, in the future I will use the MVVM pattern:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.DataVisualization.Charting.Chart miGraficoWF = new System.Windows.Forms.DataVisualization.Charting.Chart();
            System.Windows.Forms.DataVisualization.Charting.Series miSerieWF = new System.Windows.Forms.DataVisualization.Charting.Series();
            miSerieWF.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
            miGraficoWF.Series.Clear();
            miGraficoWF.Series.Add(miSerieWF);
            miSerieWF.Points.AddY(2);
            miSerieWF.Points.AddY(5);
            miSerieWF.Points.AddY(1);

            wfhGraficoWinForm.Child = miGraficoWF;
        }

      

However, the control is not displayed.

If I create a winform project and use the same code, the graph is displayed, so I believe the code that creates the chart is correct, and the problem is that I don't know how to show the winform control in a wpf view.

Thank.

+3


source to share


1 answer


The problem is not with the WPF / WinForms interop layer, but with WinForms.

You must add ChartArea

:



miGraficoWF.ChartAreas.Add("Default");

      

+3


source







All Articles