WPF设置自定义控件依赖属性无效解决方案

3 篇文章 0 订阅

[前言]
由于刚接触WPF不久,经验不足,走了很多弯路,比如本次遇到的设置自定义控件依赖属性无效问题,该依赖属性没有在自定义控件前台直接使用,只是作为一个标识通过后端代码来控制前端部分控件,依赖属性定义看起来没啥问题,属性类型为bool类型,使用的时候设置true有效false无效,段点调试false也不进入回调方法.
依赖属性代码:

  		/// <summary>
        /// 连接状态(成功/失败)
        /// </summary>
        public bool IsSucceed
        {
            get { return (bool)GetValue(IsSucceedProperty); }
            set { SetValue(IsSucceedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSucceed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSucceedProperty =
            DependencyProperty.Register("IsSucceed", typeof(bool), typeof(StateDisplayer), new PropertyMetadata(OnSetSuccessfulPropertyChangedCallback));

        private static void OnSetSuccessfulPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            StateDisplayer stateDisplayer = d as StateDisplayer;
            if (stateDisplayer != null)
            {
            if ((bool)e.NewValue)
                {
                    stateDisplayer.stateText.Text = "NO";
                    stateDisplayer.stateBorder.Background = new SolidColorBrush(Color.FromRgb(55, 176, 117));
                }
                else
                {
                    stateDisplayer.stateText.Text = "OFF";
                    stateDisplayer.stateBorder.Background = new SolidColorBrush(Color.FromRgb(225, 53, 53));
                }
            }
        }

[解决方案]
原因分析:bool类型即使不设置默认值,也会有一个默认值false,依赖属性检查属性值没有更改时则不会进入回调方法.
解决方法:1.将bool类型改为枚举或其他存在多种状态的数据类型,设置默认值与需要设置的值不一样即可
2.将bool改为可空类型,设置默认值为null(本次解决方案)
解决方案代码如下:

  		/// <summary>
        /// 连接状态(成功/失败)
        /// </summary>
        public bool? IsSucceed
        {
            get { return (bool?)GetValue(IsSucceedProperty); }
            set { SetValue(IsSucceedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSucceed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSucceedProperty =
            DependencyProperty.Register("IsSucceed", typeof(bool?), typeof(StateDisplayer), new PropertyMetadata(null, OnSetSuccessfulPropertyChangedCallback));

        private static void OnSetSuccessfulPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            StateDisplayer stateDisplayer = d as StateDisplayer;
            if (stateDisplayer != null)
            {
            if ((bool)e.NewValue)
                {
                    stateDisplayer.stateText.Text = "NO";
                    stateDisplayer.stateBorder.Background = new SolidColorBrush(Color.FromRgb(55, 176, 117));
                }
                else
                {
                    stateDisplayer.stateText.Text = "OFF";
                    stateDisplayer.stateBorder.Background = new SolidColorBrush(Color.FromRgb(225, 53, 53));
                }
            }
        }

[后语]
1.由于工作原因,没有过多时间来排版和优化控件,控件较为粗糙,内部bug可能存在很多,各位朋友同仁如发现异常请轻喷并与我及时联系,以免误导他人.
2.写该文章目的是记录自己在开发中遇到的问题,方便以后遇到类似问题以便快速解决,也是为了给有相同问题的道友提供部分思路.
本章完

获取WPF自定义控件依赖属性的数据流程如下: 1. 定义依赖属性:在自定义控件的代码中,定义一个依赖属性并注册该属性。例如: ``` public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register( "MyProperty", typeof(string), typeof(MyControl), new PropertyMetadata("Default Value")); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } ``` 2. 绑定依赖属性:在XAML中,将自定义控件依赖属性绑定到其他控件或数据源。例如: ``` <local:MyControl MyProperty="{Binding MyData}" /> ``` 3. 获取依赖属性的值:当自定义控件被渲染时,WPF框架会自动调用依赖属性的get方法,从绑定的数据源中获取属性的值。如果没有绑定任何数据源,则使用属性的默认值。例如: ``` string myPropertyValue = myControlInstance.MyProperty; ``` 4. 监听依赖属性的变化:如果需要在属性值发生变化时执行一些自定义逻辑,可以在自定义控件中注册属性值变化的回调函数。例如: ``` public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register( "MyProperty", typeof(string), typeof(MyControl), new PropertyMetadata("Default Value", OnMyPropertyChanged)); private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Execute custom logic when MyProperty value changes } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值