自定义控件填充颜色不使用依赖项属性更新

时间:2018-01-26 10:43:50

标签: c# wpf

我有一个自定义的wifi信号强度控制,我正在研究所以控制包括弧和椭圆。取决于Wifi强度的dbs,每个弧应相应填充。但是由于填充颜色没有更新,因此存在问题。任何想法都将受到高度赞赏。

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());

            }
        }
    }
}

我将它添加到MainWindow

<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>

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

1 个答案:

答案 0 :(得分:3)

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

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); }
}

Binding应该如下所示。设置UpdateSourceTrigger=PropertyChanged毫无意义。

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

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

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

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