Search code examples
wpff#windowsformshostfsxaml

Adding WindowsFormsHost throws XamlParseException


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>

Solution

  • 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