8,735
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Windows.Media.Imaging;
namespace MakePower.Controls.GraphControls
{
public partial class SwitchA : UserControl
{
//注册依赖属性
public static readonly DependencyProperty IsOnProperty =
DependencyProperty.Register("IsOn", typeof(bool), typeof(SwitchA), new PropertyMetadata(false,
new PropertyChangedCallback(OnIsOnChanged)));
//.NET属性(可选)
public bool IsOn
{
get { return (bool)GetValue(IsOnProperty); }
set { SetValue(IsOnProperty, value); }
}
//属性改变时的回调
private static void OnIsOnChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
SwitchA switchA = o as SwitchA;
switchA.Update();
}
public SwitchA()
{
InitializeComponent();
}
public void Update()
{
if (IsOn == false)
{
ellipse.Fill = new SolidColorBrush(Colors.Red);
ellipse.VerticalAlignment = VerticalAlignment.Top;
}
else
{
ellipse.Fill = new SolidColorBrush(Colors.Green);
ellipse.VerticalAlignment = VerticalAlignment.Bottom;
}
}
}
}