Custom vs User control

Choice is not only between user control and custom control, but among user control, custom control, customizing control template, customizing data template, header template (for collection based controls), attached properties. Refer to Control Authoring overview I go by following order of consideration Attached Properties : If functionality can be achieved, I use attached properties. Example, … 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 ….. />

User Control – Custom Properties

You do this via attributes on the properties, like this: [Description(“Test text displayed in the textbox”),Category(“Data”)] public string Text { get => myInnerTextBox.Text; set => myInnerTextBox.Text = value; } The category is the heading under which the property will appear in the Visual Studio Properties box. Here’s a more complete MSDN reference, including a list … Read more

How do I make an Event in the Usercontrol and have it handled in the Main Form?

You need to create an event handler for the user control that is raised when an event from within the user control is fired. This will allow you to bubble the event up the chain so you can handle the event from the form. When clicking Button1 on the UserControl, i’ll fire Button1_Click which triggers … Read more

DesignMode with nested Controls

Revisiting this question, I have now ‘discovered’ 5 different ways of doing this, which are as follows: System.ComponentModel.DesignMode property System.ComponentModel.LicenseManager.UsageMode property private string ServiceString() { if (GetService(typeof(System.ComponentModel.Design.IDesignerHost)) != null) return “Present”; else return “Not present”; } public bool IsDesignerHosted { get { Control ctrl = this; while(ctrl != null) { if((ctrl.Site != null) && ctrl.Site.DesignMode) … Read more

How can a WPF UserControl inherit a WPF UserControl?

Ensure that you have changed the first tag in the xaml to also inherit from your new basetype So <UserControl x:Class=”TestDependencyProperty827.DataTypes.DataTypeWholeNumber” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:s=”clr-namespace:System;assembly=mscorlib” > becomes <myTypes:BaseDataType x:Class=”TestDependencyProperty827.DataTypes.DataTypeWholeNumber” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:s=”clr-namespace:System;assembly=mscorlib” xmlns:myTypes=”clr-namespace:TestDependencyProperty827.DataTypes” > So, to summarise the complete answer including the extra details from the comments below: The base class should not include a xaml … Read more

‘UserControl’ constructor with parameters in C#

Design decisions made regarding the way Windows Forms works more or less preclude parameterized .ctors for windows forms components. You can use them, but when you do you’re stepping outside the generally approved mechanisms. Rather, Windows Forms prefers initialization of values via properties. This is a valid design technique, if not widely used. This has … Read more

How to create a WPF UserControl with NAMED content

The answer is to not use a UserControl to do it. Create a class that extends ContentControl public class MyFunkyControl : ContentControl { public static readonly DependencyProperty HeadingProperty = DependencyProperty.Register(“Heading”, typeof(string), typeof(MyFunkyControl), new PropertyMetadata(HeadingChanged)); private static void HeadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((MyFunkyControl) d).Heading = e.NewValue as string; } public string Heading { get; set; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)