Abstract UserControl inheritance in Visual Studio designer

What we want First, let’s define the final class and the base abstract class. public class MyControl : AbstractControl … public abstract class AbstractControl : UserControl // Also works for Form … Now all we need is a Description provider. public class AbstractControlDescriptionProvider<TAbstract, TBase> : TypeDescriptionProvider { public AbstractControlDescriptionProvider() : base(TypeDescriptor.GetProvider(typeof(TAbstract))) { } public override … Read more

WPF UserControl Design Time Size

For Blend, a little known trick is to add these attributes to your usercontrol or window: xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″ xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ mc:Ignorable=”d” d:DesignHeight=”500″ d:DesignWidth=”600″ This will set the design height and width to 500 and 600 respectively. However this will only work for the blend designer. Not the Visual Studio Designer. As far as the Visual Studio Designer … Read more

Add a user control to a wpf window

You need to add a reference inside the window tag. Something like: xmlns:controls=”clr-namespace:YourCustomNamespace.Controls;assembly=YourAssemblyName” (When you add xmlns:controls=” intellisense should kick in to make this bit easier) Then you can add the control with: <controls:CustomControlClassName ….. />

Making an indexed control array?

I know I’m a little late to this party, but this solution will work: Make a global array: TextBox[] myTextBox; Then in your object’s constructor, after the call to InitializeComponent(); initialize your array: myTextBox = new TextBox[] {TextBox1, TextBox2, … }; Now you can iterate your array of controls: for(int i = 0; i < … Read more

How to create user define (new) event for user control in WPF ?one small example

A brief example on how to expose an event from the UserControl that the main window can register: In your UserControl: 1 . Add the following declaration: public event EventHandler UserControlClicked; 2 . In your UserControl_Clicked event raise the event like this: private void UserControl_MouseDown(object sender, MouseButtonEventArgs e) { if (UserControlClicked != null) { UserControlClicked(this, … Read more

How to make user controls know about css classes in ASP.NET

Here’s what I did: <link rel=”Stylesheet” type=”text/css” href=”https://stackoverflow.com/questions/34390/Stylesheet.css” id=”style” runat=”server” visible=”false” /> It fools Visual Studio into thinking you’ve added a stylesheet to the page but it doesn’t get rendered. Here’s an even more concise way to do this with multiple references; <% if (false) { %> <link rel=”Stylesheet” type=”text/css” href=”https://stackoverflow.com/questions/34390/Stylesheet.css” /> <script type=”text/javascript” src=”js/jquery-1.2.6.js” … Read more

How to drag a UserControl inside a Canvas

This is done in silverlight and not in WPF, but it should work the same. Create two private properties on the control: protected bool isDragging; private Point clickPosition; Then attatch some event handlers in the constructor of the control: this.MouseLeftButtonDown += new MouseButtonEventHandler(Control_MouseLeftButtonDown); this.MouseLeftButtonUp += new MouseButtonEventHandler(Control_MouseLeftButtonUp); this.MouseMove += new MouseEventHandler(Control_MouseMove); Now create those methods: … Read more

How do I get the HTML output of a UserControl in .NET (C#)?

You can render the control using Control.RenderControl(HtmlTextWriter). Feed StringWriter to the HtmlTextWriter. Feed StringBuilder to the StringWriter. Your generated string will be inside the StringBuilder object. Here’s a code example for this solution: string html = String.Empty; using (TextWriter myTextWriter = new StringWriter(new StringBuilder())) { using (HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter)) { myControl.RenderControl(myWriter); html = … Read more

UserControl with header and content – Allow dropping controls in content panel and Prevent dropping controls in header at design time

You should do the following : For your user control, you need to create a new designer which enables the inner panel on design-time by calling EnableDesignMode method. For the inner panel, you need to create a designer which disables moving, resizing and removes some properties from designer. You should register the designers. Example You … Read more