加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 19:47:08 所属栏目:百科 来源:网络整理
导读:public partial class OmenLevel : UserControl { public OmenLevel() { InitializeComponent(); } #region 属性 public static readonly DependencyProperty _LevelValue = DependencyProperty.Register("LevelValue",typeof(Int32),typeof(OmenLevel)); //
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("设定值不在有效范围内!rn有效值范围:0[无]1[轻微]2[中等]3[严重]");
                }
            }
        }
        #endregion

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读