您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 机械/模具设计 > WPF设计自定义控件
在实际工作中,WPF提供的控件并不能完全满足不同的设计需求。这时,需要我们设计自定义控件。这里LZ总结一些自己的思路,特性如下:CouplingUITemplateBehaviourFunctionPackage下面举例说说在项目中我们经常用到调音台音量条,写一个自定义控件模拟调音台音量条。自定义控件SingnalLight,实现功能接收来自外部的范围0~100的数值实时显示接收数值数值范围0~50显示绿色,50~85显示黄色,85~100显示红色,没有数值显示褐色可在父控件上拖拽该控件首先NewWPFApplicationProject,在Ui上放2个Button,代码:1Grid2StackPanelOrientation=HorizontalVerticalAlignment=Bottom3ButtonContent=StartClick=Start_Click/Button4ButtonContent=StopClick=Stop_Click/Button5/StackPanel6/GridStart,Stop事件实现1privatevoidStart_Click(objectsender,RoutedEventArgse){2SignalManager.Instance.Start();3}45privatevoidStop_Click(objectsender,RoutedEventArgse){6SignalManager.Instance.Stop();7}这里创建一个SignalManager类,在Start时开启一个计时器,每隔1秒生成一个0~100的随机数,并作为模拟数值输出。SignalManager类代码:1namespaceSignalLightDemo.Business{2publicclassSignalManager:DependencyObject{3publicstaticSignalManagerInstance{get{returninstance;}}45publicintRandomA{6get{return(int)GetValue(RandomAProperty);}7set{SetValue(RandomAProperty,value);}8}910SignalManager(){11InitializationTimer();12}1314publicvoidStart(){15if(!timerA.Enabled)timerA.Start();16}1718publicvoidStop(){19if(timerA.Enabled)timerA.Stop();20}2122privatevoidInitializationTimer(){23timerA=newTimer();24timerA.Interval=INTERVAL;25timerA.Elapsed+=timerA_Elapsed;26}2728voidtimerA_Elapsed(objectsender,ElapsedEventArgse){29this.Dispatcher.BeginInvoke(newAction(()={30RandomA=a.Next(MAX_VALUE);31}));32}3334publicstaticreadonlyDependencyPropertyRandomAProperty=35DependencyProperty.Register(RandomA,typeof(int),typeof(SignalManager),newPropertyMetadata(0));3637privateRandoma=newRandom((int)DateTime.Now.Ticks);38privateconstintMAX_VALUE=100;39privateconstdoubleINTERVAL=1000;40privateTimertimerA;41privatestaticSignalManagerinstance=newSignalManager();42}43}下面来重点:1.创建自定义控件SingnalLight1publicclassSingnalLight:ContentControl{2publicintValueA{3get{return(int)GetValue(ValueAProperty);}4set{SetValue(ValueAProperty,value);}5}678publicSingnalLight(){9this.AllowDrop=true;10}111213staticSingnalLight(){14DefaultStyleKeyProperty.OverrideMetadata(typeof(SingnalLight),newFrameworkPropertyMetadata(typeof(SingnalLight)));15}161718}ValueA为接受外部数值的属性2.复写控件UITemplate1StyleTargetType={x:Typecontrol:SingnalLight}2SetterProperty=RenderTransform3Setter.Value4TranslateTransformX={BindingPath=X,RelativeSource={RelativeSourceAncestorType={x:Typecontrol:SingnalLight}}}5Y={BindingPath=Y,RelativeSource={RelativeSourceAncestorType={x:Typecontrol:SingnalLight}}}/6/Setter.Value7/Setter8SetterProperty=Template9Setter.Value10ControlTemplate11ControlTemplate.Resources12control:SingnalLightStatusConverterx:Key=colorconverter/control:SingnalLightStatusConverter13control:SingnalLightValueConverterx:Key=valueconverter/control:SingnalLightValueConverter14/ControlTemplate.Resources15StackPanel16TextBlockText={BindingPath=ValueA,RelativeSource={RelativeSourceAncestorType={x:Typecontrol:SingnalLight}}}/TextBlock17TextBlockText=100/TextBlock18Border19x:Name=bd120Height={BindingPath=LightHeight,RelativeSource={RelativeSourceAncestorType={x:Typecontrol:SingnalLight}}}21SnapsToDevicePixels=True22BorderBrush=BlackBorderThickness=1Background=Transparent23RectangleFill={BindingPath=ValueA,24RelativeSource={RelativeSourceAncestorType={x:Typecontrol:SingnalLight}},25Converter={StaticResourceResourceKey=colorconverter}}26VerticalAlignment=Bottom27Rectangle.Height28MultiBindingConverter={StaticResourceResourceKey=valueconverter}29BindingPath=ValueARelativeSource={RelativeSourceAncestorType={x:Typecontrol:SingnalLight}}/Binding30BindingPath=HeightElementName=bd1/Binding31/MultiBinding32/Rectangle.Height33/Rectangle34/Border35TextBlockText=0/TextBlock36/StackPanel37/ControlTemplate38/Setter.Value39/Setter40/Style3.接受值判断,SingnalLight通过实现IValueConverter和OverrideArrange&MeasureMethods,实现了UI呈现的绑定,1publicclassSingnalLightStatusConverter:IValueConverter{2publicobjectConvert(objectvalue,TypetargetType,objectparameter,System.Globalization.CultureInfoculture){3SolidColorBrushresult=Brushes.Transparent;4if(value.GetType()==typeof(int)){5varcolor=System.Convert.ToInt32(value);6if(color50)result=Brushes.Green;7elseif(color85&&color=50)result=Brushes.Yellow;8elseif(color=100&&color=85)result=Brushes.Red;9elseresult=Brushes.Gray;10}11returnresult;12}1314publicobjectConvertBack(objectvalue,TypetargetType,objectparameter,System.Globalization.CultureInfoculture){15thrownewNotImplementedException();16}17}1819publicclassSingnalLightValueConverter:IMultiValueConverter{20publicobjectConvert(object[]values,TypetargetType,objectparameter,System.Globalization.CultureInfoculture){21doubleresult=0;22if(values[0].GetType()==typeof(int)&&values[1].GetType()==typeof(double)){23result=(double)values[1]/100*System.Convert.ToDouble(values[0]);24}25returnresult;26}2728publicobject[]ConvertBack(objectvalue,Type[]targetTypes,objectparameter,System.Globalization.CultureInfoculture){29thrownewNotImplementedException();30}31}1protectedover
本文标题:WPF设计自定义控件
链接地址:https://www.777doc.com/doc-2867960 .html