将 windows.forms 控件添加到 wpf

本文关键字:wpf 添加 控件 windows forms | 更新日期: 2023-09-27 18:37:08

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

我得到了这个代码

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
                <WindowsFormsHost Margin="272,10,396,42" Width="240">
                    <wf:TextBox x:Name="txtAutoProductCode" AutoCompleteMode="SuggestAppend" AutoCompleteSource="CustomSource" />
                </WindowsFormsHost>

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

详细的例外情况如下。.

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.<.ctor>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)

将 windows.forms 控件添加到 wpf

我能让它工作的唯一方法是在代码隐藏中设置自动完成属性。否则我会看到同样的错误。但它必须在 InitializeComponent() 之后完成。

public MainWindow()
{
    InitializeComponent();
    txtAutoProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
    txtAutoProductCode.AutoCompleteCustomSource.Add("item1");
    txtAutoProductCode.AutoCompleteCustomSource.Add("item2");
}
<Grid>
    <WindowsFormsHost Margin="272,10,396,42" Width="240">
        <wf:TextBox x:Name="txtAutoProductCode" AutoCompleteMode="SuggestAppend"/>
    </WindowsFormsHost>
</Grid>

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

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

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

XAML

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
                <WindowsFormsHost Margin="272,10,396,42" Width="240">
                    <wf:TextBox x:Name="txtAutoProductCode" />
                </WindowsFormsHost>

自动完成功能

    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();
    }

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

运行它。