Custom Control Fill Color not updating with Dependency Property

前端 未结 1 1678
忘了有多久
忘了有多久 2021-01-26 20:20

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.

相关标签:
1条回答
  • 2021-01-26 20:51

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

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

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

    or

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

    or

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

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

    0 讨论(0)
提交回复
热议问题