1.UI代码
<Button Background="Black" BorderBrush="Transparent" Padding="0" HorizontalContentAlignment="Left" Style="{StaticResource ButtonEmpty}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:Valve1h}}, Path=ValueCmd}"
CommandParameter="{Binding ElementName=ID,Path=Text }" Cursor="Hand" IsHitTestVisible="True"/>
注释:属性绑定释义
Command--Ancestor 查找绑定至自定义控件Valve1h,实体命令绑定本地自建依赖属性ValueCmd
CommandParameter--绑定本地TextBlock控件Name为ID,Path为其文本值
2.cs代码
2.1 string依赖属性及属性值变化回调函数
public static readonly DependencyProperty ValueLabelProperty =
DependencyProperty.Register("ValueLabel", typeof(string),
typeof(Valve1h), new PropertyMetadata("00", new
PropertyChangedCallback(OnLabelChanged)));//CHANGED
public string ValueLabel
{
get { return (string)GetValue(ValueLabelProperty); }
set { SetValue(ValueLabelProperty, value); }
}
static void OnLabelChanged(object sender, DependencyPropertyChangedEventArgs args)
{
Valve1h source = (Valve1h)sender;//CHANGED
source.ID.Text = (string)args.NewValue;//CHANGED
}
2.2 Bool依赖属性及属性值变化回调函数
public static readonly DependencyProperty ValueBitProperty =
DependencyProperty.Register("ValueBit", typeof(bool),
typeof(Valve1h), new PropertyMetadata(false, new PropertyChangedCallback(OnBitChanged)));//CHANGED--属性变化回调函数
public bool ValueBit
{
get { return (bool)GetValue(ValueBitProperty); }
set { SetValue(ValueBitProperty, value); }
}
static void OnBitChanged(object sender, DependencyPropertyChangedEventArgs args)
{
Valve1h source = (Valve1h)sender;//CHANGED
if ((bool)args.NewValue)
source.ValveShape.Fill = Brushes.GreenYellow;
else
source.ValveShape.Fill = Brushes.SteelBlue; //CHANGED
}
2.3 Command依赖属性
public static readonly DependencyProperty ValueCmdProperty =
DependencyProperty.Register("ValueCmd", typeof(ICommand),
typeof(Valve1h), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });//CHANGED
public ICommand ValueCmd
{
get { return (ICommand)GetValue(ValueCmdProperty); }
set { SetValue(ValueCmdProperty, value); }
}