欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

wpf 两个自定义控件

程序员文章站 2023-09-07 00:00:11
wpf 两个自定义控件 一个是IP控件,一个滑动条。先看下效果图 IPControl 1、实际工作中有时需要设置IP信息,就想着做一个ip控件。效果没有window自带的好,需要通过tab切换。但也能满足使用。废话不多说直接上代码 IPControl.xaml IPControl.xaml.cs 2 ......

wpf 两个自定义控件

一个是ip控件,一个滑动条。先看下效果图

wpf 两个自定义控件

ipcontrol

1、实际工作中有时需要设置ip信息,就想着做一个ip控件。效果没有window自带的好,需要通过tab切换。但也能满足使用。废话不多说直接上代码

wpf 两个自定义控件

ipcontrol.xaml
<usercontrol x:class="wpfapp1.ipcontrol"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:wpfapp1">

    <viewbox>
        <border>
            <dockpanel>
                <dockpanel.resources>
                    <style targettype="{x:type textbox}">
                        <setter property="horizontalcontentalignment" value="center"/>
                        <setter property="verticalcontentalignment" value="top"/>
                        <setter property="textalignment" value="center"/>
                        <setter property="borderthickness" value="0"/>
                        <setter property="horizontalalignment" value="left"/>
                        <setter property="verticalalignment" value="stretch"/>
                        <setter property="maxlength" value="3"/>
                        <setter property="width" value="30"/>
                        <setter property="height" value="25"/>
                    </style>
                    <style targettype="{x:type label}">
                        <setter property="content" value="."/>
                    </style>
                </dockpanel.resources>
                <textbox tabindex="1" gotfocus="textboxgotfocus" text="{binding path=firstipvalue, relativesource={relativesource findancestor, ancestortype={x:type local:ipcontrol}}, updatesourcetrigger=propertychanged}"/>
                <label/>
                <textbox tabindex="2" gotfocus="textboxgotfocus" text="{binding path=secondipvalue, relativesource={relativesource findancestor, ancestortype={x:type local:ipcontrol}}, updatesourcetrigger=propertychanged}"/>
                <label/>
                <textbox tabindex="3" gotfocus="textboxgotfocus" text="{binding path=thirdipvalue, relativesource={relativesource findancestor, ancestortype={x:type local:ipcontrol}}, updatesourcetrigger=propertychanged}"/>
                <label/>
                <textbox tabindex="4" gotfocus="textboxgotfocus" text="{binding path=forthipvalue, relativesource={relativesource findancestor, ancestortype={x:type local:ipcontrol}}, updatesourcetrigger=propertychanged}"/>
            </dockpanel>
        </border>
    </viewbox>
</usercontrol>
ipcontrol.xaml.cs
    public partial class ipcontrol : usercontrol
    {
        public ipcontrol()
        {
            initializecomponent();
        }


        #region  dependencyproperty

        public static readonly dependencyproperty ipaddressproperty =
            dependencyproperty.register("ipaddress", typeof(string), typeof(ipcontrol), new propertymetadata(defaultip, (d, e) =>
            {
                if (d is ipcontrol control)
                {
                    control.updateparts(control);
                }
            }));

        public string ipaddress
        {
            get { return (string)getvalue(ipaddressproperty); }
            set { setvalue(ipaddressproperty, value); }
        }

        #endregion


        #region static field

        private static readonly string defaultip = "127.0.0.1";

        #endregion


        #region field

        private string firstipvalue;
        private string secondipvalue;
        private string thirdipvalue;
        private string forthipvalue;

        #endregion


        #region property

        public string firstipvalue
        {
            get { return firstipvalue; }
            set
            {
                if (firstipvalue != value)
                {
                    updateiptext(value, 1, ref firstipvalue);
                }
            }
        }

        public string secondipvalue
        {
            get { return secondipvalue; }
            set
            {
                if (secondipvalue != value)
                {
                    updateiptext(value, 0, ref secondipvalue);
                }
            }
        }

        public string thirdipvalue
        {
            get { return thirdipvalue; }
            set
            {
                if (thirdipvalue != value)
                {
                    updateiptext(value, 0, ref thirdipvalue);
                }
            }
        }

        public string forthipvalue
        {
            get { return forthipvalue; }
            set
            {
                if (forthipvalue != value)
                {
                    updateiptext(value, 0, ref forthipvalue);
                }
            }
        }

        #endregion


        #region private method

        private void textboxgotfocus(object sender, routedeventargs e)
        {
            inputmethod.current.imestate = inputmethodstate.off;
            textbox tb = sender as textbox;
            if (tb.text.length != 0)
            {
                tb.selectall();
            }
        }

        private void updateiptext(string oldvalue, int minvalue, ref string newvalue)
        {
            int.tryparse(oldvalue, out int ivalue);
            if (ivalue < minvalue)
            {
                ivalue = minvalue;
            }
            if (ivalue > 255)
            {
                ivalue = 255;
            }
            newvalue = ivalue.tostring();
            ipaddress = getipaddress();
        }

        private string getipaddress()
        {
            string str = "";
            if (firstipvalue != null && firstipvalue.length > 0)
            {
                str += firstipvalue + ".";
            }
            else
            {
                str += "0.";
            }
            if (secondipvalue != null && secondipvalue.length > 0)
            {
                str += secondipvalue + ".";
            }
            else
            {
                str += "0.";
            }
            if (thirdipvalue != null && thirdipvalue.length > 0)
            {
                str += thirdipvalue + ".";
            }
            else
            {
                str += "0.";
            }
            if (forthipvalue != null && forthipvalue.length > 0)
            {
                str += forthipvalue;
            }
            else
            {
                str += "0";
            }
            return str;
        }

        private void updateparts(ipcontrol control)
        {
            if (control.ipaddress == null)
            {
                control.ipaddress = defaultip;
            }
            string[] parts = control.ipaddress.split('.');
            if (parts.length == 4)
            {
                control.firstipvalue = parts[0];
                control.secondipvalue = parts[1];
                control.thirdipvalue = parts[2];
                control.forthipvalue = parts[3];
            }
        }

        #endregion

    }

2、控件有4个textbox、4个label组成。textbox显示ip值,label显示ip数据的“.”。

textbox绑定依赖属性,设置tabindex参数,通过tab按键切换到下一个textbox。每个textbox最多输入3位

lightcontrol

1、前段时间,领导紧急安排一个工作。做一个测试灯光的小软件。与负责灯光同事沟通得知,光源板可同时控制24路灯。也就是说软件界面上需要有24个scrollbar用来表示灯光亮度,24个label显示名称。这要是一个一个控件加太慢了,没法做一个自定义空间,可设置显示名称,通过滑动条或者直接设置参数,改变亮度。于是需要一个label、一个scrollbar、一个textbox与scrollbar关联。

wpf 两个自定义控件

lightcontrol.xaml
<usercontrol x:class="wpfapp1.lightcontrol"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:wpfapp1">

    <viewbox>
        <border>
            <dockpanel>
                <label content="{binding path=labelcontent, relativesource={relativesource findancestor, ancestortype={x:type local:lightcontrol}}}"
                       fontsize="17" width="80" verticalcontentalignment="center"/>
                <scrollbar value="{binding path=lightvalue, relativesource={relativesource findancestor, ancestortype={x:type local:lightcontrol}}}"
                           orientation="horizontal" height="40" width="200" maximum="100" smallchange="1"/>
                <textbox text="{binding path=lightvalue, stringformat={}{0:f4}, relativesource={relativesource findancestor, ancestortype={x:type local:lightcontrol}}, updatesourcetrigger=propertychanged}"
                         fontsize="17" width="80" verticalcontentalignment="center"/>
            </dockpanel>
        </border>
    </viewbox>
</usercontrol>
lightcontrol.xaml.cs
    public partial class lightcontrol : usercontrol
    {
        public lightcontrol()
        {
            initializecomponent();
        }


        #region  dependencyproperty

        public static readonly dependencyproperty labelcontentproperty =
            dependencyproperty.register("labelcontent", typeof(string), typeof(lightcontrol), new propertymetadata("灯光"));

        public string labelcontent
        {
            get { return (string)getvalue(labelcontentproperty); }
            set { setvalue(labelcontentproperty, value); }
        }


        public static readonly dependencyproperty lightvalueproperty =
            dependencyproperty.register("lightvalue", typeof(double), typeof(lightcontrol), new propertymetadata(1.0));

        public double lightvalue
        {
            get { return (double)getvalue(lightvalueproperty); }
            set { setvalue(lightvalueproperty, value); }
        }

        #endregion


    }

2、label显示名称通过依赖属性由外接传入。

3、scrollbar的value属性与textbox的text属性绑定同一个依赖属性,可传递到调用者,同时textbox显示信息设置保留小数点4位。

工作中有时需要自己做一些自定义控件,用来满足不同场景的需求。两个小控件,比较简单,希望此文能提供一些思路给你。