当前位置: 首页> 健康> 养生 > C#--WPF自定义控件模板示例

C#--WPF自定义控件模板示例

时间:2025/8/22 17:01:58来源:https://blog.csdn.net/BlueCapt/article/details/139113862 浏览次数:0次

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)));//CHANGEDpublic string ValueLabel{get { return (string)GetValue(ValueLabelProperty); }set { SetValue(ValueLabelProperty, value); }}static void OnLabelChanged(object sender, DependencyPropertyChangedEventArgs args){Valve1h source = (Valve1h)sender;//CHANGEDsource.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;//CHANGEDif ((bool)args.NewValue)source.ValveShape.Fill = Brushes.GreenYellow;elsesource.ValveShape.Fill = Brushes.SteelBlue;            //CHANGED}
2.3 Command依赖属性
        public static readonly DependencyProperty ValueCmdProperty =DependencyProperty.Register("ValueCmd", typeof(ICommand),typeof(Valve1h), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });//CHANGEDpublic ICommand ValueCmd{get { return (ICommand)GetValue(ValueCmdProperty); }set { SetValue(ValueCmdProperty, value); }}

关键字:C#--WPF自定义控件模板示例

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: