wpf中的自定义控件的问题 [英] problem with custom control in wpf

查看:109
本文介绍了wpf中的自定义控件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi工程师
我是wpf的新手,在阅读了一些文章之后,我尝试制作一个从文本框派生的测试自定义控件.
我想制作一个带有圆角的文本框,因此我的自定义控件具有一个名为CornerRadius的属性,该属性需要一个整数. 在generic.xaml文件中,我编写了覆盖我的自定义控件的默认模板的代码行,请查看下面的代码以了解我的所作所为(我现在不信任或不信任)


mycustomControl.cs代码:

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)));
        }
    }




和generic.xaml:




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>




在我的示例中,我定义了一个名为CornerRadius的DependencyProperty,它需要一个整数来使边界边缘变圆,但不能正常工作,
请告诉我我的虫子在哪里?




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.

推荐答案

CornerRadius .尝试将属性的类型更改为该类型.

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


这篇关于wpf中的自定义控件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆