如何在WPF中使用自定义控件 [英] How to use custom Controls in WPF

查看:81
本文介绍了如何在WPF中使用自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在C#中创建了一个自定义控件(Button控件中的重写方法并添加了新事件).我需要在我的 wpf 应用程序中使用此控件.在 WinForms 中,我可以通过 ToolBox(右键单击)->使用它.选择项目->浏览.在WPF中,我无法导入自定义控件.有什么办法做到这一点

I have created a custom control in C# ( Overridden methods in Button control and added new events) . I need to use this control in my wpf application. In WinForms i can use this by ToolBox(right click) --> Choose Items -->Browse. where as in WPF i can not import the custom controls. Is there any way to do this

推荐答案

可能需要为特定项目进行重建,然后xaml文件应该处于活动状态.您的自定义控件应出现在工具箱中.如果没有显示.您可以执行以下操作:

might need a rebuild for the certain project then a xaml file should be active. Your custom control should appear in the toolbox. if it doesn't show. you can do the following:

在您的xaml文件中的标头标记中的某个位置,您会在其中看到许多xmlns:yyy,然后添加一个新的xmlns:例如:

in your xaml file, somewhere in the header tag, where you see many of the xmlns:yyy, add a new xmlns: for example:

<Window
    x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:MyProject">

其中custom是名称空间的任何标识符,而MyProject是名称空间.不用担心,因为当您键入"xmlns:custom =(不带引号)时,intellisense将为您提供当前引用的现有名称空间的选择.因此只需从下拉列表中选择适当的名称空间,然后按Enter.

where custom is any identifier for the name space and MyProject is the namespace. Dont worry about this because when you type "xmlns:custom=" (without the quotes) intellisense will give you choice of existing namespaces currently referenced. so just choose the appropriate namespace from the drop down and press enter.

现在向下滚动到要放置自定义控件的位置,然后:

now scroll down to where you want to put your custom control and:

<custom:MyControl Content="Click Me!" Click="Button_Click" />

custom:MyControl来自xmlns: custom (上方)和控件名称 MyControl Content 是分配给"Click Me!"的Content属性.而 Click 是带有处理程序"Button_Click"的Click事件.

custom:MyControl is from the xmlns:custom (above) and the Control name, MyControl Content is the Content property assigned "Click Me!" and Click is the Click event with the handler "Button_Click".

希望这会有所帮助!

这篇关于如何在WPF中使用自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆