将windows.forms控件添加到wpf

时间:2013-10-28 08:57:31

标签: c# wpf winforms

任何将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)

2 个答案:

答案 0 :(得分:1)

我能让它工作的唯一方法是在代码后面设置自动完成属性。否则我看到同样的错误。但必须在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>

答案 1 :(得分:0)

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

添加Auto_Complete();在InitializeComponent();

之后

运行它。

相关问题