【WPF】自定义控件之依赖属性
发布时间:2020-05-22 18:38:56 所属栏目:程序设计 来源:互联网
导读:public partial class OmenLevel : UserControl { public OmenLevel() { InitializeComponent(); } #region 属性 public static readonly Dependen
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
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
