2010-11-08 19 views
2

所以我試圖用使用一個WinForms自定義WPF控件:如何修復'指定的類名'WpfSample.MainWindow'與實際的根實例類型'System.Windows.Window'錯誤不匹配?

var f = new FileStream (@"C:\sample.xaml", FileMode.Open, FileAccess.Read); 
var element = XamlReader.Load(f); 

f.Close(); 

var elementHost = new ElementHost(); 
elementHost.Dock = System.Windows.Forms.DockStyle.Fill; 
elementHost.Child = element; 
this.Controls.Add(elementHost); 

我不是在WPF經歷,所以我不知道如何解決這個錯誤。

編輯:

所以這是XAML文件:

<Window x:Class="WpfSample.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:r="http://schemas.aspitalia.com/Ricciolo/Controls" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.Resources> 


      <XmlDataProvider x:Key="data" Source="Data.xml"> 
      </XmlDataProvider> 

      <HierarchicalDataTemplate x:Key="dt" ItemsSource="{Binding XPath=outline}"> 
      </HierarchicalDataTemplate> 

     </Grid.Resources> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 

     <r:TreeListView ItemsSource="{Binding Source={StaticResource data},XPath=/opml/body/outline/outline}" ItemTemplate="{StaticResource dt}"> 
      <r:TreeListView.Columns> 
       <GridViewColumn Header="Title" DisplayMemberBinding="{Binding [email protected]}" Width="150" /> 
       <GridViewColumn Header="Childs" DisplayMemberBinding="{Binding [email protected]}" Width="100" /> 
       <GridViewColumn Header="Url" DisplayMemberBinding="{Binding [email protected]}" Width="200" /> 
      </r:TreeListView.Columns> 
     </r:TreeListView> 


    </Grid> 
</Window> 

我也有這個從網上自由定製treelistview控制,並且既包括編譯控制,並在data.xml中這個xaml使用的解決方案。

EDIT2:背後

代碼MainWindow.xaml:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfSample 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow () 
     { 
      InitializeComponent (); 
     } 
    } 
} 
+0

問題是(可能)因爲'sample.xaml'和後面的代碼。你能否提供這兩件物品? – 2010-11-08 19:43:07

+0

謝謝,稍後會發布,但後面的代碼是標準的,即我什麼也沒有改變。 – 2010-11-08 20:50:02

+1

那是什麼語言?這顯然不是C#,它也不是VB ... – 2010-11-08 21:03:46

回答

0

確定。我認爲問題來自於你從一個文件加載的事實,但支持它的Window類(即對象後面的代碼)沒有被引入。

您應該在與WinForms應用程序相同的程序集中定義WPF控件(不知道是否應該使用WPF窗口 - 也許嘗試使用UserControl instread?),或者只需添加對包含WPF UserControl的程序集的引用,並把它連接起來。

無論哪種方式,你應該只能夠說

elementHost.Child = new WpfSample.MainWindow(); 
this.Controls.Add(elementHost); 

希望這可以讓你關閉。

相關問題