[英]Custom Control Fill Color not updating with Dependency Property
我有一個自定義的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>
但它沒有更新它的顏色。 這段代碼有什么問題。 任何想法謝謝
您必須遵守依賴項屬性命名約定,其中DependencyProperty標識符字段的名稱類似於具有Property
后綴的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); }
}
然后,綁定應如下所示。 設置UpdateSourceTrigger=PropertyChanged
是沒有意義的。
Fill="{Binding ElementName=_this, Path=State1Color}"
要么
Fill="{Binding State1Color, ElementName=_this}"
要么
Fill="{Binding State1Color, RelativeSource={RelativeSource AncestorType=UserControl}}"
我還建議將屬性重命名為StateXBrush
而不是StateXColor
,因為它們是Brushes,而不是Colors。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.