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 }