将windows.forms控件添加到wpf

任何添加windows.forms控件到xaml的想法。

我有这个代码

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"    

但我有一个例外。 我不知道该怎么做。 我被困在这里。

详细的例外情况如下。

 System.Windows.Markup.XamlParseException was unhandled HResult=-2146233087 Message='Initialization of 'Billing.MainWindow' threw an exception.' Line number '6' and line position '9'. Source=PresentationFramework LineNumber=6 LinePosition=9 StackTrace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc) at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties) at System.Windows.Application.DoStartup() at System.Windows.Application.b__1(Object unused) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 

只有我可以让它工作的方法是在代码后面设置自动完成属性。 否则我看到同样的错误。 但必须在InitializeComponent()之后完成。

 public MainWindow() { InitializeComponent(); txtAutoProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource; txtAutoProductCode.AutoCompleteCustomSource.Add("item1"); txtAutoProductCode.AutoCompleteCustomSource.Add("item2"); }      

正如@WeylandYutani所说,我从xaml代码中删除了自动完成属性。 它工作正常。

首先添加对WindowsFormsIntegration和System.Windows.Forms的引用

然后使用System.Windows.Forms作为命名空间添加

XAML

  xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"    

自动完成function

  void Auto_Complete() { txtAutoProductCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend; txtAutoProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource; AutoCompleteStringCollection coll = new AutoCompleteStringCollection(); SqlCeCommand com = new SqlCeCommand("SELECT ProductCode FROM Products_Master", con); SqlCeDataReader dr; con.Open(); try { dr = com.ExecuteReader(); while (dr.Read()) { string aProduct = dr.GetString(0); coll.Add(aProduct); } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } txtAutoProductCode.AutoCompleteCustomSource = coll; con.Close(); } 

添加Auto_Complete(); 在InitializeComponent()之后;

运行。