选中对象【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 }
文章结束给大家分享下程序员的一些笑话语录:
看新闻说中国输入法全球第一!领先了又如何?西方文字根本不需要输入法。一点可比性都没有。