自定义控件填充颜色不随依赖属性更新 [英] Custom Control Fill Color not updating with Dependency Property

查看:25
本文介绍了自定义控件填充颜色不随依赖属性更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 wifi 信号强度控件,我正在处理,因此控件由弧形和椭圆形组成.取决于 Wifi 强度的 dbs,每个弧应相应地填充.但是存在一个问题,因为填充颜色没有更新.任何想法将不胜感激.

I have a custom wifi signal strength control, i am working on so the control comprise of arcs and an ellipse. Depends on dbs of Wifi strength, each arc should fill accordingly. But there is a problem as the fill color is not updating. Any idea would be highly appreciated.

XAML

<UserControl x:Class="CustomChartControl.WifiControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CustomChartControl"
             mc:Ignorable="d" 

            xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
            x:Name="_this"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

        <ed:Arc x:Name="Rate1" Stroke="Gray" ArcThicknessUnit="Pixel" StrokeThickness="1" StartAngle="313" EndAngle="47" Margin="109,110,111,121" Width="80" Stretch="None" ArcThickness="8" Fill="{Binding ElementName=_this,Path=state4Color,UpdateSourceTrigger=PropertyChanged}"/>
        <ed:Arc x:Name="Rate2" Stroke="Gray" ArcThicknessUnit="Pixel" StrokeThickness="1" StartAngle="320" EndAngle="40" Margin="120,124,120,112" Width="60" Stretch="None" ArcThickness="8" Fill="{Binding ElementName=_this,Path=state3Color, UpdateSourceTrigger=PropertyChanged}"/>
        <ed:Arc x:Name="Rate3" Stroke="Gray" ArcThicknessUnit="Pixel" StrokeThickness="1" StartAngle="330" EndAngle="30" Margin="130,138,130,104" Width="40" Stretch="None" ArcThickness="8" Fill="{Binding ElementName=_this,Path=state2Color, UpdateSourceTrigger=PropertyChanged}"/>
        <Ellipse x:Name="Rate4" Stroke="Gray" StrokeThickness="1" Width="8" Height="8" Margin="146,150,146,142" Fill="{Binding ElementName=_this,Path=state1Color,UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</UserControl>

代码隐藏

namespace CustomChartControl
{
    /// <summary>
    /// Interaction logic for WifiControl.xaml
    /// </summary>
    public partial class WifiControl : UserControl
    {
        private static readonly DependencyProperty State1Color = DependencyProperty.Register("State1Color", typeof(Brush), typeof(WifiControl));
        public Brush state1Color
        {
            get { return (Brush)this.GetValue(State1Color); }
            set { this.SetValue(State1Color, value); }

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

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

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

        }
        public int WifiStrength { get; set; }

        public WifiControl()
        {
            InitializeComponent();
        }

        private void WifiSignalStrength()
        {
            var searcher=new ManagementObjectSearcher(@"root\WMI", "select Ndis80211ReceivedSignalStrength from MSNdis_80211_ReceivedSignalStrength where active=true");
            ManagementObjectCollection Adapters = searcher.Get();
            foreach(ManagementObject mo in Adapters)
            {
                WifiStrength = Convert.ToInt32(mo["Ndis80211ReceivedSignalStrength"].ToString());

            }
        }
    }
}

我把它添加到主窗口

<Window>
<Grid>
        <local:WifiControl HorizontalAlignment="Left" Height="312" Margin="709,196,0,0" VerticalAlignment="Top" Width="309" state1Color="Blue" state2Color="Cyan",state3Color="Red",state4Color="Orange" />

    </Grid>
</Window>

但它没有更新它的颜色.这段代码有什么问题.任何想法谢谢

But its not updating its colors. Whats wrong with this code. Any Idea Thanks

推荐答案

您必须遵守依赖属性命名约定,其中 DependencyProperty 标识符字段的命名类似于带有 Property 后缀的属性:

You must adhere to dependency property naming conventions, where the DependencyProperty identifier field is named like the property with a Property suffix:

private static readonly DependencyProperty State1ColorProperty =
    DependencyProperty.Register(nameof(State1Color), typeof(Brush), typeof(WifiControl));

public Brush State1Color
{
    get { return (Brush)GetValue(State1ColorProperty); }
    set { SetValue(State1ColorProperty, value); }
}

绑定应该如下所示.设置 UpdateSourceTrigger=PropertyChanged 毫无意义.

The Binding should then look like shown below. Setting UpdateSourceTrigger=PropertyChanged is pointless.

Fill="{Binding ElementName=_this, Path=State1Color}"

Fill="{Binding State1Color, ElementName=_this}"

Fill="{Binding State1Color, RelativeSource={RelativeSource AncestorType=UserControl}}"

<小时>

我还建议将您的属性重命名为 StateXBrush 而不是 StateXColor,因为它们是画笔,而不是颜色.


I'd also recommand to rename your properties to StateXBrush instead of StateXColor, because they are Brushes, not Colors.

这篇关于自定义控件填充颜色不随依赖属性更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆