How to use custom Controls in 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
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:
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">
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 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".
Hope this helps!
I take it you are talking about a WinForms user control here, not a WPF one? If so, I'd suggest you read the following article: http://msdn.microsoft.com/en-us/library/ms742875.aspx
I might add that it may be a good idea to simply create a WPF user control - the process has improved much compared to WinForms, also the content model should make things quite a bit easier.
精彩评论