0

So I made a Custom LED and it has DependencyProperty called LEDState. So now in Main .xml file ( the one that uses my Custom LED) is do LEDState = "1", the LED was change color. I can even Change the LED Color in runtime. but if I tried to Blind the LED It will ever display a color. Now I was using a devexpress LED and I had it Blind to the same variable and it worked good. So I know my Code outside my custom LED is working. So there is something wrong with my Custom LED. here is my Code :

    public int LEDState
    {
        get { return (int)GetValue(LEDStateProperty); }
        set
        {
            SetValue(LEDStateProperty, value);

        }
    }

    // Using a DependencyProperty as the backing store for LEDState.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LEDStateProperty =
       DependencyProperty.Register("LEDState", typeof(int), typeof(UserControl1 ), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender, (o, e) => ((UserControl1)o).OnLEDStateChanged()));



    private  void OnLEDStateChanged()
    {
        SetLEDState(LEDState);
    }

here is my .Xml code for the Main :

   <LEDControl:UserControl1 Grid.Column="1" Grid.Row="4" x:Name="Output1"   LEDState ="3" HorizontalAlignment="Left" VerticalAlignment="Top" Width="25" Height="24" />
            <LEDControl:UserControl1 Grid.Column="2" Grid.Row="4" x:Name="Output2"   LEDState ="{Binding OutputNoOFFIndex2}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="25" Height="24" />

Again the one with LED= "6" will work but the Blinding one with not. Also, this is the blinding I used for devexpress :

and that works ...

So there is something missing in my LED class

Also I did try to do what they said here : WPF UserControl with ObservableCollection DependencyProperty not binding

but it is still not working. There is something missing in my LED Class... to make it working with Blinding, but i do not know what it is.

Also, I did try to removed this.DataContext = this;

but that did not help at all ...

also I have a devexpress LED that is working Great with the same variable. So it is definitely something wrong with my LED class.

here is the Code for the DevExpress LED :

<dxga:StateIndicatorControl Grid.Column="0" Grid.Row="4" x:Name="OutputNoOFF2" HorizontalAlignment="Left" StateIndex="{Binding OutputNoOFFIndex2}" VerticalAlignment="Top" Width="25" Height="24">
                <dxga:StateIndicatorControl.DefaultState>
                    <dxga:State/>
                </dxga:StateIndicatorControl.DefaultState>
                <dxga:StateIndicatorControl.Model>
                    <dxga:LampStateIndicatorModel/>
                </dxga:StateIndicatorControl.Model>
            </dxga:StateIndicatorControl>

Update : here is the LED XML file :

<UserControl x:Class="LEDControl.UserControl1"
         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:LEDControl"
         x:Name="LEDControlWindow"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">

<Grid>
    <Viewbox>
        <Canvas Height="100" Width="100">
            <Ellipse Canvas.Left="0" Canvas.Top="0" Width="100" Height="100" StrokeThickness="0.5" Stroke="Gray">
                <Ellipse.Fill>
                    <LinearGradientBrush x:Name="LinearProcessBrush" StartPoint="0.5,1" EndPoint="1,0" SpreadMethod="Pad">
                        <GradientStop Offset="0" />
                        <GradientStop Color="White" Offset="1" />
                    </LinearGradientBrush>
                </Ellipse.Fill>
            </Ellipse>

            <Ellipse Canvas.Left="7.5" Canvas.Top="7.25" Width="85" Height="85" StrokeThickness="0.5" Stroke="Gray">
                <Ellipse.Fill>
                    <LinearGradientBrush x:Name="LinearProcessBrush2" StartPoint="1,0.5" EndPoint="0,1" SpreadMethod="Pad">
                        <GradientStop Color="#FF071923" Offset="0" />
                        <GradientStop Color="White" Offset="1" />
                    </LinearGradientBrush>
                </Ellipse.Fill>
            </Ellipse>



            <Ellipse Canvas.Left="12.25" Canvas.Top="12.25" Width="75" Height="75" StrokeThickness="0.5" Stroke="Black">
                <Ellipse.Fill>
                    <RadialGradientBrush Center="0.6, 0.35" GradientOrigin="0.6,0.3" RadiusY="0.67" RadiusX="0.67">
                        <RadialGradientBrush.RelativeTransform>
                            <TransformGroup>
                                <ScaleTransform CenterY="0.35" CenterX="0.6" ScaleY="1" ScaleX="1" />
                                <SkewTransform AngleY="0" AngleX="0" CenterY="0.35" CenterX="0.6" />
                                <RotateTransform Angle="-4.447" CenterY="0.35" CenterX="0.6" />
                                <TranslateTransform X="0" Y="0"/>
                            </TransformGroup>
                        </RadialGradientBrush.RelativeTransform>
                        <GradientStop Color="White" Offset="0"/>
                        <GradientStop Color="{Binding Col, Mode=OneWay, UpdateSourceTrigger=PropertyChanged }" Offset="1"/>
                    </RadialGradientBrush>
                </Ellipse.Fill>
            </Ellipse>


        </Canvas>
        </Viewbox>
</Grid>

here is the SetLEDSate function :

    public void SetLEDState( int state)
    {

        switch (state)
        {
            case 1:

                Col = Colors.LimeGreen;

                break;
            case 2:
                Col = Colors.Red;
                break;
            case 3:
                Col = Colors.Yellow;
                break;

            default:
                Col = Colors.Yellow;
                break;
        }


    }




    private System.Windows.Media.Color _Col;
    public System.Windows.Media.Color Col
    {
        get { return _Col; }
        set
        {
            if (value != _Col)
            {

                _Col = value;
                onPropertyChanged("Col");
            }


        }
    }
Echo
  • 11
  • 1
  • When you refer to other StackOverflow posts, please inlcude a link. For the DataContext, there must not be any explicit assignment to it in the UserControl, neither in its XAML nor in its code behind. – Clemens Jan 17 '20 at 15:00
  • Clemens right now I can change the color by doing LEDState="1" but if I remove This.Datacontext = this; even that stops working. And the Binding does not work either way. Do you know what I should do to get the binding to work ? – Echo Jan 17 '20 at 15:27
  • If you explicitly assign the UserControl's DataContext, Bindings like `LEDState ="{Binding OutputNoOFFIndex2}"` will not work, because they expect their source property in the current DataContext, i.e. the view model object. Besides that, we can't tell why your control won't work without setting its own DataContext. Perhaps there are Bindings in its own XAML that operate on an internal DataContext. We don't know because you didn't show us that. – Clemens Jan 17 '20 at 18:24
  • Clemens I just added the LED class Xml file to the post – Echo Jan 17 '20 at 19:19
  • The Binding should be `Color="{Binding Col, RelativeSource={RelativeSource AncestorType=UserControl}}"`. `Mode=TwoWay` and `UpdateSourceTrigger=PropertyChanged` is nonsense here. – Clemens Jan 17 '20 at 19:29
  • You wouldn't even need the Binding to an internal property like Col at all (and hence also not an implementation of INotifyPropertyChanged). Just assign a name to the GradientStop, e.g. `` and access it from code behind: `gradientStop.Color = Colors.LimeGreen;`. – Clemens Jan 17 '20 at 19:32
  • Clemens oh wow that worked ... why did it work ? – Echo Jan 17 '20 at 19:43

0 Answers0