【WPF】自定义控件之依赖属性

public partial class OmenLevel : UserControl
    {
        public OmenLevel()
        {
            InitializeComponent();
        }

        #region 属性
        public static readonly DependencyProperty _LevelValue = DependencyProperty.Register("LevelValue",typeof(Int32),typeof(OmenLevel));
        /// <summary>
        /// 征兆等级值[有效值范围:0[无]1[轻微]2[中等]3[严重]]
        /// </summary>
        public int LevelValue
        {
            get
            {
                return (int)GetValue(_LevelValue);
            }
            set
            {
                if (value >=0 && value<4)
                {
                    SetValue(_LevelValue,value);
                    for (int i = 0; i < 4; i++)//将非选中对象重置为认背景色,将选中对象设置为选中背景色
                    {
                        Button btn = (Button)this.FindName("btn" + i);//查找按钮对象
                        if (btn != null)
                        {
                            LinearGradientBrush LGBrush = (LinearGradientBrush)btn.Background;
                            if (i == value)
                            {//修改当前选中的按钮的渐变停止背景色
                                LGBrush.GradientStops[1].Color = Color.Fromrgb(21,190,241);
                            }
                            else
                            {
                                LGBrush.GradientStops[1].Color = Color.Fromrgb(130,181,229);
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("设定值不在有效范围内!\r\n有效值范围:0[无]1[轻微]2[中等]3[严重]");
                }
            }
        }
        #endregion

        #region 私有方法
        private void Button_Click(object sender,RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            LevelValue = Convert.ToInt32(btn.Tag);
        }
        #endregion
    }

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...