WPF-22:WPF繪製五角星改進版(增加半個五角星的繪製)-修改bug

來源:互聯網
上載者:User
之前用座標畫多邊形的方法,繪製五角星。今天調試時發現當時寫的時候有bug,修改一下。
原文:http://blog.csdn.net/yysyangyangyangshan/article/details/9313421,當時沒測試綁定的問題,一測試發現綁定有問題。原來是多顆五角星控制項中,相依性屬性的typeof寫錯了類。
SelectCount和ItemsCount的typeof(FivePointStar)應該為typeof(FivePointStarGroup)才對,順便將屬性的賦值裡的代碼改為在回調裡調用。
修改後如下:
    /// <summary>    /// FivePointStarGroup.xaml 的互動邏輯    /// </summary>    public partial class FivePointStarGroup : UserControl    {        //預設值        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);        private static event DependencyPropertyChangedEventHandler PropertyChangedEvent;        private static ObservableCollection<FivePointStarModel> data = new ObservableCollection<FivePointStarModel>();        public FivePointStarGroup()        {            InitializeComponent();            this.Loaded += new RoutedEventHandler(FivePointStarGroup_Loaded);            PropertyChangedEvent -= new DependencyPropertyChangedEventHandler(FivePointStarGroup_PropertyChangedEvent);            PropertyChangedEvent += new DependencyPropertyChangedEventHandler(FivePointStarGroup_PropertyChangedEvent);            this.lsbGroups.ItemsSource = data;        }          /// <summary>        /// 五角星半徑        /// </summary>        public double Radius        {            get             {               object result = GetValue(RadiusProperty);                if(result==null)                {                    return radius;                }                return (double)result;            }            set { SetValue(RadiusProperty, value);}        }        public static  DependencyProperty RadiusProperty =           DependencyProperty.Register("Radius", typeof(double),           typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintRadiusElementControls));        public static bool PaintRadiusElementControls(object value)        {            if (PropertyChangedEvent != null)            {                PropertyChangedEvent(1, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));            }            return true;        }        /// <summary>        /// 五角星個數        /// </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 static  DependencyProperty ItemsCountProperty =           DependencyProperty.Register("ItemsCount", typeof(double),           typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintItemCountElementControls));        public static bool PaintItemCountElementControls(object value)        {            if (PropertyChangedEvent != null)            {                PropertyChangedEvent(2, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));            }            return true;        }        /// <summary>        /// 選中的五角星個數        /// </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);}        }        public static  DependencyProperty SelectCountProperty =           DependencyProperty.Register("SelectCount", typeof(double),           typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintSelectCountElementControls));        public static bool PaintSelectCountElementControls(object value)        {            if (PropertyChangedEvent != null)            {                PropertyChangedEvent(3, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));            }            return true;        }        /// <summary>        /// 滑鼠點擊選中事件        /// </summary>        public event RoutedEventHandler SelectCountChangeEvent        {            add { AddHandler(SelectCountChangePropertyEvent, value); }            remove { RemoveHandler(SelectCountChangePropertyEvent, value); }        }        public static RoutedEvent SelectCountChangePropertyEvent =          EventManager.RegisterRoutedEvent("SelectCountChangeEvent",          RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Control));        /// <summary>        /// 選中顏色        /// </summary>        public Brush SelectBackground        {            get            {                object result = GetValue(SelectBackgroundProperty);                if (result == null)                {                    return selectBackground;                }                return (Brush)result;            }            set {  SetValue(SelectBackgroundProperty, value);  }        }        public static  DependencyProperty SelectBackgroundProperty =           DependencyProperty.Register("SelectBackground", typeof(Brush),           typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintSelectGroundElementControls));        public static bool PaintSelectGroundElementControls(object value)        {            if (PropertyChangedEvent != null)            {                PropertyChangedEvent(4, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));            }            return true;        }        /// <summary>        /// 未選中顏色        /// </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(PaintUnSelectGroundElementControls));        public static bool PaintUnSelectGroundElementControls(object value)        {            if (PropertyChangedEvent != null)            {                PropertyChangedEvent(5, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));            }            return true;        }        /// <summary>        /// 回調時繪圖事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        void FivePointStarGroup_PropertyChangedEvent(object sender, DependencyPropertyChangedEventArgs e)        {            if (sender == null || e == 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>        /// 繪圖        /// </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);                //在此設定星形顯示的顏色                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>        /// 滑鼠選中五角星        /// </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));         }    }

修改後工程下載(包括了綁定測試):http://download.csdn.net/detail/yysyangyangyangshan/5782113

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.