Custom controls are user-defined controls that can be created to encapsulate specific functionality or behavior.
Define Simple Custom Control
Let's assume we are creating custom Input Box Control for our form since we have multiple input fields in our form and we want to have same look and feel for all input fields. So we can create a custom control for input box which will have a label and a textbox.
So let's create new custom control InputBox in UserControls folder and add User Control WPF item to it. It will create a new InputBox.xaml file and InputBox.xaml.cs file.
Add this code in InputBox.xaml file to create a custom input box control with label and textbox.
To use this custom control in your main window, you need to add the control namespace in your main window XAML
<Windowx:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp"mc:Ignorable="d"xmlns:userControls="clr-namespace:WpfApp.UserControls"Title="MainWindow"Height="450"Width="800"><Grid><Grid.ColumnDefinitions><ColumnDefinitionWidth="*"/><ColumnDefinitionWidth="200"/></Grid.ColumnDefinitions><!-- Add Stack Panel in first column --><StackPanelGrid.Column="0"Background="White"><!-- Use InputBox UserControl --><userControls:InputBoxx:Name="NameInputBox"Label="Name"Text="John Doe"/><userControls:InputBoxx:Name="AgeInputBox"Label="Age"Text="30"/><userControls:InputBoxx:Name="BalanceInputBox"Label="Balance"Text="1000"/></StackPanel><!-- Add Stack Panel in second column --><StackPanelGrid.Column="1"Background="LightGray"><ButtonName="RunButton"Content="Run"Click="RunButton_Click"Margin="10"/></StackPanel></Grid></Window>
Code behind to use this custom control in your main window will have the following code.
To define an event in a custom control, you can create a public event in the code-behind file of the custom control. For example, let's say we want to raise an event when the text in the ValueTextBox changes. We can define an event called TextChanged in the InputBox.xaml.cs file.
publicpartialclassInputBox:UserControl{publicInputBox(){InitializeComponent();ValueTextBox.TextChanged+=ValueTextBox_TextChanged;}publicstringLabel{get=>LabelTextBlock.Text;set=>LabelTextBlock.Text=value;}publicstringText{get=>ValueTextBox.Text;set=>ValueTextBox.Text=value;}// Define the TextChanged eventpubliceventEventHandlerTextChanged;privatevoidValueTextBox_TextChanged(objectsender,TextChangedEventArgse){// Raise the TextChanged event when the text changesTextChanged?.Invoke(this,EventArgs.Empty);}}
Dependency Properties in Custom Control
Dependency properties are a special type of property in WPF that provide additional functionality, such as data binding, styling, triggers and animation. To define a dependency property in a custom control, you can use the DependencyProperty.Register method. For example, let's say we want to create a dependency property called LabelText in the InputBox control.
publicpartialclassInputBox:UserControl{publicInputBox(){InitializeComponent();}publicstringLabel{get=>LabelTextBlock.Text;set=>LabelTextBlock.Text=value;}publicstringText{get=>ValueTextBox.Text;set=>ValueTextBox.Text=value;}// Define the LabelText dependency propertypublicstaticreadonlyDependencyPropertyLabelTextProperty=DependencyProperty.Register("LabelText",typeof(string),typeof(InputBox),newPropertyMetadata(string.Empty,OnLabelTextChanged));publicstringLabelText{get=>(string)GetValue(LabelTextProperty);set=>SetValue(LabelTextProperty,value);}privatestaticvoidOnLabelTextChanged(DependencyObjectd,DependencyPropertyChangedEventArgse){varcontrol=dasInputBox;if(control!=null){control.Label=e.NewValueasstring;}}}