Previously, we used coordinates to draw a polygon. When debugging today, we found that there was a bug at the time of writing. modify it.
Original article: Workshop. It turns out that the typeof dependency attribute of multiple star control has an error class.
The typeof (fivepointstar) of selectcount and itemscount should be set to typeof (fivepointstargroup). By the way, the code in the attribute assignment should be changed to called in the callback.
The modification is as follows:
/// <Summary> /// fivepointstargroup. interaction logic of XAML // </Summary> Public partial class fivepointstargroup: usercontrol {// default value: Private Static Double Radius = 20; Private Static double itemscount = 5; private Static double selectcount = 5; Private Static brush selectbackground = new solidcolorbrush (colors. yellowgreen); Private Static brush unselectbackgroud = new solidcolorbrush (colors. darkgray); priv Ate static event implements propertychangedevent; Private Static observablecollection <fivepointstarmodel> DATA = new observablecollection <fivepointstarmodel> (); Public fivepointstargroup () {initializecomponent (); this. loaded + = new routedeventhandler (fivepointstargroup_loaded); propertychangedevent-= new dependencypropertychangedeventhandler (fivepointstargroup_property Changedevent); propertychangedevent + = new dependencypropertychangedeventhandler (fivepointstargroup_propertychangedevent); this. lsbgroups. itemssource = data ;}/// <summary> /// five-star radius /// </Summary> Public Double Radius {get {object result = getvalue (radiusproperty ); if (result = NULL) {return radius;} return (double) result;} set {setvalue (radiusproperty, value) ;}} public static dependencypro Perty radiusproperty = dependencyproperty. register ("radius", typeof (double), typeof (fivepointstargroup), new uipropertymetadata (), new validatevaluecallback (callback); public static bool paintradiuselementcontrols (object value) {If (propertychangedevent! = NULL) {propertychangedevent (1, new dependencypropertychangedeventargs (selectcountproperty, null, value);} return true ;} /// <summary> /// number of five-pointed stars /// </Summary> Public double itemscount {get {object result = getvalue (itemscountproperty ); if (result = NULL | convert. todouble (result) <= 0) {return itemscount;} return (double) Result ;}set {setvalue (itemscountproperty, value) ;}} public sta TIC dependencyproperty itemscountproperty = dependencyproperty. register ("itemscount", typeof (double), typeof (fivepointstargroup), new uipropertymetadata (), new validatevaluecallback (paintitemcountelementcontrols); public static bool aggregate (object value) {If (propertychangedevent! = NULL) {propertychangedevent (2, new dependencypropertychangedeventargs (selectcountproperty, null, value);} return true ;} /// <summary> /// number of selected five-pointed stars /// </Summary> Public double selectcount {get {object result = getvalue (selectcountproperty ); if (result = NULL | convert. todouble (result) <= 0) {return selectcount;} return (double) Result ;}set {setvalue (selectcountproperty, value) ;}} pub LIC static dependencyproperty selectcountproperty = dependencyproperty. register ("selectcount", typeof (double), typeof (fivepointstargroup), new uipropertymetadata (), new validatevaluecallback (paintselectcountelementcontrols); public static bool values (object value) {If (propertychangedevent! = NULL) {propertychangedevent (3, new dependencypropertychangedeventargs (selectcountproperty, null, value);} return true ;} /// <summary> /// click the selected event with the mouse /// </Summary> public event routedeventhandler selectcountchangeevent {Add {addhandler (selectcountchangepropertyevent, value );} remove {removehandler (selectcountchangepropertyevent, value) ;}} public static routedevent selectcountchangepropert Yevent = eventmanager. registerroutedevent ("selectcountchangeevent", routingstrategy. bubble, typeof (routedeventhandler), typeof (Control )); /// <summary> /// selected color /// </Summary> Public brush selectbackground {get {object result = getvalue (selectbackgroundproperty); If (result = NULL) {return selectbackground;} return (Brush) result;} set {setvalue (selectbackgroundproperty, value) ;}} public s Tatic dependencyproperty selectbackgroundproperty = dependencyproperty. register ("selectbackground", typeof (Brush), typeof (fivepointstargroup), new uipropertymetadata (), new validatevaluecallback (paintselectgroundelementcontrols); public static bool evaluate (object value) {If (propertychangedevent! = NULL) {propertychangedevent (4, new dependencypropertychangedeventargs (selectcountproperty, null, value);} return true ;} /// <summary> /// unselected color /// </Summary> Public brush unselectbackground {get {object result = getvalue (unselectbackgroundproperty); If (result = NULL) {return unselectbackgroud;} return (Brush) result;} set {setvalue (unselectbackgroundproperty, value) ;}} public static Dependencyproperty unselectbackgroundproperty = dependencyproperty. register ("unselectbackground", typeof (Brush), typeof (fivepointstargroup), new uipropertymetadata (), new validatevaluecallback (callback); public static bool evaluate (object value) {If (propertychangedevent! = NULL) {propertychangedevent (5, new dependencypropertychangedeventargs (selectcountproperty, null, value);} return true ;} /// <summary> /// drawing event during callback /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> void fivepointstargroup_propertychangedevent (Object sender, dependencypropertychangedeventargs e) {If (sender = NULL | E. newvalue = NULL) {return;} int flag = convert. toint32 (sender); Switch (FLAG) {Case 1: initialdata (convert. todouble (E. newvalue), this. itemscount, this. selectcount, this. selectbackground, this. unselectbackground); break; Case 2: initialdata (this. radius, convert. todouble (E. newvalue), this. selectcount, this. selectbackground, this. unselectbackground); break; Case 3: initialdata (this. radius, this. itemscount, convert. todouble (E. newvalue), this. selectbackground, this. unselectbackground); break; Case 4: initialdata (this. radius, this. itemscount, this. selectcount, E. newvalue as brush, this. unselectbackground); break; Case 5: initialdata (this. radius, this. itemscount, this. selectcount, this. selectbackground, E. newvalue as brush); break;} invalidatevisual ();} void fivepointstargroup_loaded (Object sender, routedeventargs e) {initialdata (this. radius, this. itemscount, this. selectcount, this. selectbackground, this. unselectbackground );} /// <summary> /// drawing /// </Summary> /// <Param name = "R"> </param> /// <Param name =" itemcount "> </param> // <Param name =" selectcount "> </param> // <Param name =" selectbackground "> </param> /// <Param name = "unselectbackground"> </param> Private Static void initialdata (Double R, double itemcount, double selectcount, brush selectbackground, brush unselectbackground) {data. clear (); int COUNT = convert. toint32 (itemcount); If (count <= 0) {COUNT = convert. toint32 (itemscount) ;}for (INT I = 0; I <count; I ++) {fivepointstarmodel item = new fivepointstarmodel (); item. id = I + 1; item. radius = r; item. selectbackground = selectbackground; item. unselectbackgroud = unselectbackground; item. margins = New thickness (R, 0, R, 0); // set the star color here if (I + 1)> selectcount & (I + 1-selectcount)> 0) & (I + 1-selectcount) <1) {item. currentvalue = 0.5;} else if (I + 1)> selectcount) {item. currentvalue = 0;} else {item. currentvalue = 1;} data. add (item );}} /// <summary> /// select the pentagram with the mouse /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void fivepointstar_mousedown (Object sender, mousebuttoneventargs e) {fivepointstar M = sender as fivepointstar; If (M = NULL) {return;} int Index = convert. toint32 (M. tag); this. selectcount = index; raiseevent (New routedeventargs (selectcountchangepropertyevent, Sender ));}}
Modified project download (includes binding test): http://download.csdn.net/detail/yysyangyangyangshan/5782113