2

I was trying to add a Line Chart using FSharp.Charting in MainWindow. In MainViewModel I have -

let chart = Chart.Line [ for i in 0 .. 10 -> (i, i * i) ]
let control = new ChartControl(chart)
member self.LineChart = control

and in xaml -

<WindowsFormsHost Child="{Binding LineChart}" />

When I start the application, I get XamlParseException with following additional information -

'Cannot create unknown type '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}WindowsFormsHost'.' Line number '20' and line position '10'.

How to solve the problem?


Here it is @Foggy Finder. I have removed few lines defining TextBlock and Buttons.

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
xmlns:local="clr-namespace:ViewModels;assembly=AsyncFS"
xmlns:fsxaml="http://github.com/fsprojects/FsXaml"
Title="MVVM and XAML Type provider" Height="200" Width="400">
<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <WindowsFormsHost Child="{Binding LineChart}" />
</Grid>
FoggyFinder
  • 2,230
  • 2
  • 20
  • 34
  • 1
    Why not use `OxyPlot` or `LiveCharts`? – FoggyFinder Nov 30 '16 at 23:16
  • 1
    Can you show full XAML (or at least its title) ? – FoggyFinder Nov 30 '16 at 23:19
  • `FSharpCharting` seems easier than `LiveChart`. I am not yet familiar with OxyPlot` @FoggyFinder –  Nov 30 '16 at 23:30
  • 2
    I'm not sure if this is relevant for this Q but [windowsformshost](http://stackoverflow.com/questions/37382234/adding-windowsformshost-control-to-mainwindow-causes-to-crash-the-exe/) has some sample code. I have added in the past FSharp Charts to WPF. It flickers a lot, you will be better of using native WPF like oxyplot or livecharts. – s952163 Dec 01 '16 at 00:25
  • @s952163 livechart did not render bars at all for some volume of stock trade in one of my C# Test project where DevExpress chart worked fine. livechart's animation is better than that of devexpress. In case of small and medium sized array I will use livechart. –  Dec 01 '16 at 01:05
  • @EmonHaque yes, I wouldn't be surprised if you come across some bugs there, I only tested so can't comment much. – s952163 Dec 01 '16 at 01:42

1 Answers1

0

In order to use WinfowsFormsHost you need to add a reference to WindowsFormsIntegration. But if you do it you get :

A 'Binding' cannot be set on the 'Child' property of type 'WindowsFormsHost'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

Easy way to fix is to create the object directly:

<ContentControl Content="{Binding LineChart}" />

...

member self.LineChart = new WindowsFormsHost(Child = control)

Result:

enter image description here

FoggyFinder
  • 2,230
  • 2
  • 20
  • 34