hi Engineers
i am new in wpf and after reading some articles,i try to make a test custom control that derived from textbox.
i want to make a textbox with rounded corners,so my custom control has a property named CornerRadius that takes an integer number for my purpose.
in the generic.xaml file ,i write code lines that overrides the default template of my custom control,please look at the bellow codes to underestand what i do(i don't now that is trust or no)
mycustomControl.cs code:
public class CustomTextBox : TextBox
{
public int CornerRadius
{
get { return (int)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(int), typeof(CustomTextBox), new FrameworkPropertyMetadata(5, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnCornerRadiusChanged), new CoerceValueCallback(CoerceCornerRadiusCallBack)), new ValidateValueCallback(IsValidRadius));
public static void OnCornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs arg)
{
}
public static object CoerceCornerRadiusCallBack(DependencyObject d, object value)
{
int tmp = (int)value;
if (tmp > 10)
{
return 10;
}
else if (tmp < 1)
{
return 1;
}
else
{
return tmp;
}
}
public static bool IsValidRadius(object value)
{
try
{
int.Parse(value.ToString());
return true;
}
catch (Exception)
{
return false;
}
}
static CustomTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTextBox), new FrameworkPropertyMetadata(typeof(CustomTextBox)));
}
}
and generic.xaml :
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomTextBox">
<Style TargetType="{x:Type local:CustomTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomTextBox}">
<Border Name="border1"
CornerRadius="{TemplateBinding CornerRadius}"
removed="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBox BorderThickness="0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
in my sample,i define a DependencyProperty named CornerRadius that takes an integer number for rounding border edges but not work properly,
please tell me where is my bug?
thanks alot.