Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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:
C#
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 :
C#
<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.
Posted
Updated 28-Apr-12 21:09pm
v3

1 solution

The Border.CornerRadius property's type is CornerRadius. Try to change the type of your property to that type.

 
Share this answer
 
Comments
HOSSEIN.AB 29-Apr-12 3:29am    
thanks alot dear friend,
HOSSEIN.AB 29-Apr-12 4:12am    
how can i define a conveter to convert an integer number to my CornerRadius type?
Shmuel Zang 29-Apr-12 5:20am    
You can define converter by implementing the IValueConverter interface. But, why do you want to do that? By setting your property's type to CornerRadius, you can set a different radius for each corner (and can also set one number that is applied to the whole of the corners).
HOSSEIN.AB 30-Apr-12 22:25pm    
i want to learn more.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900