You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义控件填充颜色未随Dependency Property更新问题排查

问题分析与解决方案

让我们一步步拆解你代码里的问题,帮你解决WiFi控件颜色无法更新的问题:

1. 依赖属性命名不符合WPF规范(核心问题)

WPF依赖属性有严格的命名规则:

  • 静态依赖属性字段必须命名为[PropertyName]Property(比如State1ColorProperty)
  • CLR包装器的属性名要和注册依赖属性时传入的名称完全一致(大小写也要匹配)

你的代码里犯了两个关键错误:

  • 静态字段名没有加Property后缀(比如你用了State1Color而不是State1ColorProperty)
  • CLR属性名是小写开头(state1Color),但注册依赖属性时用的是大写开头的名称("State1Color"),导致WPF的绑定系统无法正确关联依赖属性。

修正后的依赖属性代码:

public static readonly DependencyProperty State1ColorProperty = 
    DependencyProperty.Register("State1Color", typeof(Brush), typeof(WifiControl));
public Brush State1Color
{
    get { return (Brush)GetValue(State1ColorProperty); }
    set { SetValue(State1ColorProperty, value); }
}

public static readonly DependencyProperty State2ColorProperty = 
    DependencyProperty.Register("State2Color", typeof(Brush), typeof(WifiControl));
public Brush State2Color
{
    get { return (Brush)GetValue(State2ColorProperty); }
    set { SetValue(State2ColorProperty, value); }
}

public static readonly DependencyProperty State3ColorProperty = 
    DependencyProperty.Register("State3Color", typeof(Brush), typeof(WifiControl));
public Brush State3Color
{
    get { return (Brush)GetValue(State3ColorProperty); }
    set { SetValue(State3ColorProperty, value); }
}

public static readonly DependencyProperty State4ColorProperty = 
    DependencyProperty.Register("State4Color", typeof(Brush), typeof(WifiControl));
public Brush State4Color
{
    get { return (Brush)GetValue(State4ColorProperty); }
    set { SetValue(State4ColorProperty, value); }
}

同时要更新XAML里的绑定路径,匹配修正后的属性名:

<ed:Arc x:Name="Rate1" ... Fill="{Binding ElementName=_this, Path=State4Color, UpdateSourceTrigger=PropertyChanged}"/>
<ed:Arc x:Name="Rate2" ... Fill="{Binding ElementName=_this, Path=State3Color, UpdateSourceTrigger=PropertyChanged}"/>
<ed:Arc x:Name="Rate3" ... Fill="{Binding ElementName=_this, Path=State2Color, UpdateSourceTrigger=PropertyChanged}"/>
<Ellipse x:Name="Rate4" ... Fill="{Binding ElementName=_this, Path=State1Color, UpdateSourceTrigger=PropertyChanged}"/>

2. 主窗口XAML语法错误

你在引用控件时,属性之间用了逗号分隔,这是不符合XAML语法的,应该用空格分隔:
错误代码:

<local:WifiControl ... state1Color="Blue" state2Color="Cyan",state3Color="Red",state4Color="Orange" />

修正后:

<local:WifiControl ... State1Color="Blue" State2Color="Cyan" State3Color="Red" State4Color="Orange" />

(注意这里也要用修正后的大写开头的属性名)

3. WifiSignalStrength方法从未被调用,且未根据信号强度更新颜色

你的代码里定义了WifiSignalStrength方法来获取信号强度,但没有在任何地方调用它,而且获取到WifiStrength后,也没有根据这个值去更新四个颜色属性。

解决步骤:

  • 首先,把WifiStrength改成依赖属性,这样当它变化时可以自动触发颜色更新:
public static readonly DependencyProperty WifiStrengthProperty = 
    DependencyProperty.Register("WifiStrength", typeof(int), typeof(WifiControl), 
        new PropertyMetadata(0, OnWifiStrengthChanged));

public int WifiStrength
{
    get { return (int)GetValue(WifiStrengthProperty); }
    set { SetValue(WifiStrengthProperty, value); }
}

private static void OnWifiStrengthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = d as WifiControl;
    control.UpdateWifiColors((int)e.NewValue);
}
  • 然后添加UpdateWifiColors方法,根据信号强度设置对应的颜色(你可以根据实际dB值区间调整逻辑):
private void UpdateWifiColors(int signalStrength)
{
    // 一般WiFi信号dB值范围:-30(极好)到-90(极差),这里示例仅供参考
    var inactiveColor = Brushes.Gray;
    var activeColor = Brushes.Green;

    State1Color = signalStrength >= -80 ? activeColor : inactiveColor;
    State2Color = signalStrength >= -70 ? activeColor : inactiveColor;
    State3Color = signalStrength >= -60 ? activeColor : inactiveColor;
    State4Color = signalStrength >= -50 ? activeColor : inactiveColor;
}
  • 最后,在合适的时机调用WifiSignalStrength方法,比如构造函数初始化+定时器定期刷新:
public WifiControl()
{
    InitializeComponent();
    // 初始化获取一次信号强度
    WifiSignalStrength();
    // 用DispatcherTimer每2秒刷新一次信号强度
    var timer = new System.Windows.Threading.DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(2);
    timer.Tick += (s, e) => WifiSignalStrength();
    timer.Start();
}

4. 补充:WMI查询的异常处理

WifiSignalStrength方法里的WMI查询可能会抛出异常(比如没有找到WiFi适配器),建议添加异常处理避免崩溃:

private void WifiSignalStrength()
{
    try
    {
        var searcher = new ManagementObjectSearcher(@"root\WMI", 
            "select Ndis80211ReceivedSignalStrength from MSNdis_80211_ReceivedSignalStrength where active=true");
        ManagementObjectCollection adapters = searcher.Get();
        foreach (ManagementObject mo in adapters)
        {
            if (int.TryParse(mo["Ndis80211ReceivedSignalStrength"]?.ToString(), out int strength))
            {
                WifiStrength = strength;
            }
        }
    }
    catch (Exception ex)
    {
        // 处理异常,比如设置默认信号强度
        WifiStrength = -90;
    }
}

把这些步骤都修正后,你的WiFi控件应该就能根据信号强度正确更新颜色了。

内容的提问来源于stack exchange,提问作者Zia UsafXai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:41:24