This is a migrated thread and some comments may be shown as answers.

Add Custom Properties

1 Answer 976 Views
WatermarkTextBox
This is a migrated thread and some comments may be shown as answers.
gerardo
Top achievements
Rank 1
Veteran
gerardo asked on 08 Oct 2020, 04:07 AM

Good day,

Is there a way I can add custom property to RadWatermarkTextBox? For example adding isRequired property and use it in the control template.

 

Thank you.

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 09 Oct 2020, 06:01 AM

Hello Gerardo,

To achieve your requirement, you can create a custom class that derives from RadWatermarkTextBox and add the custom property there. After this, you can extract the default ControlTemplate of RadWatermarkTextBox and use the new property in the template.

For example:

public class CustomWatermarkTextBox : RadWatermarkTextBox
{
	public bool IsRequired
	{
		get { return (bool)GetValue(IsRequiredProperty); }
		set { SetValue(IsRequiredProperty, value); }
	}

	public static readonly DependencyProperty IsRequiredProperty =
		DependencyProperty.Register(
			"IsRequired", 
			typeof(bool), 
			typeof(CustomWatermarkTextBox), new PropertyMetadata(false));
}

<Window.Resources>
	<ControlTemplate x:Key="CustomWatermarkTextBoxTemplate" TargetType="local:CustomWatermarkTextBox">
		<!-- some elements here -->
		
		<!-- an element in the template that uses the new property -->
		<CheckBox IsChecked="{TemplateBinding IsRequired}" />
		
		<!-- some other elements here -->
	</ControlTemplate>

	<Style TargetType="local:CustomWatermarkTextBox">
		<Setter Property="Template" Value="{StaticResource CustomWatermarkTextBoxTemplate}" />
	</Style>
</Window.Resources>

Regards,
Martin Ivanov
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

Tags
WatermarkTextBox
Asked by
gerardo
Top achievements
Rank 1
Veteran
Answers by
Martin Ivanov
Telerik team
Share this question
or