Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Databind Value property of LED to a boolean variable in code

Greetings,

 

I am trying to bind the "Value" property of an LED control to a boolean property in my code (VB.net), so that when the boolean property in the code changes, the LED value reflects the change, hence automatically changing the color of the LED on the UI.  I've looked at every example out there that I can find and I cannot get this to work.   In the xml code below, "Sta1Mode" is the boolean property in my VB code.

 <ni:LED x:Name="NiLED1" HorizontalAlignment="Left" Height="23" Margin="191.973,373.934,0,0" VerticalAlignment="Top" Width="23" FalseBrush="#FF3A3A3C" TrueBrush="#FFC8D10A" Style="{DynamicResource LEDStyle1}" IsTabStop="False" Focusable="False" Value="{Binding Sta1Mode, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"/>

Can someone please post a simple example on how to do this and/or show me what's wrong in my code?

 

Thanks,

D

 

0 Kudos
Message 1 of 8
(3,084 Views)

Hey busarider,

 

What environment are you using? In something like Visual Studio/Measurement Studio, you can manually bind data with objects.

 

Bdog

0 Kudos
Message 2 of 8
(3,052 Views)

I am traveling this month, so I won't be able to test  anything directly, but your XAML looks correct to bind Value to a property named "Sta1Mode" on the WPF LED control.  Some things you might check:

- Is Sta1Mode a dependency property, or use INotifyPropertyChanged?

- Is the DataContext set to your object?  (Since an explicit Source is not given, WPF will look for an "Sta1Mode" property on whatever the data context is set to.)

 

This data binding article may be useful: https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-wpf

 

 

~ Paul H
0 Kudos
Message 3 of 8
(3,035 Views)

Paul,

 

I've followed some of the online examples that I have found, including calling a method INotifyPropertyChanged whenever the boolean property "Sta1Mode" changes.  Do I still require a "dependency" property?  From one research source I found, the target element needs to be a dependency property (?), that being the Led control's boolean value (?).  Is this required?  I do not have any properties set as dependency properties, source or target. Yes, the DataContext is set to the object.  I should note that I'm doing most of the WPF side of things in Blend.

 

Here is screenshot of the LED control properties where it shows the DataContext set to the object (VariablesForPLC) in MS Blend.  That object has a boolean property called "Sta1Mode" that I'm setting in code.  When I set that property, I want the LED control value to change accordingly.

NiLED1 Properties.jpg

Setting databinding of LED value property to "Sta1Mode" 

NiLED1 Properties2.jpg

 

 

0 Kudos
Message 4 of 8
(2,989 Views)

The settings in the screenshot look correct. Could you post the XAML that gets generated for that object?

 

Do I still require a "dependency" property?  From one research source I found, the target element needs to be a dependency property (?), that being the Led control's boolean value (?).  Is this required?


Yes, to create a WPF binding at least one of the properties involved needs to be a WPF dependency property. In this case, the Value property on the LED should serve that purpose.

 

I've followed some of the online examples that I have found, including calling a method INotifyPropertyChanged whenever the boolean property "Sta1Mode" changes.


If VariablesForPLC implements INotifyPropertyChanged, and you are raising the PropertyChanged event using a method whenever Sta1Mode changes, then that should be all you need to do for the binding to trigger.

~ Paul H
0 Kudos
Message 5 of 8
(2,984 Views)

Click event of this button control sets the boolean property : Sta1Mode

NiLED1 Properties4.jpgT

 

The VariablesForPLC object's property Sta1Mode.  I've stepped through this code via debug and when the button is clicked, it is setting this property (T or F) so this appears to be working.  Is it actually raising the inotifiypropertychanged event?  It runs the line of code and no exception errors are thrown, so I assume it is (?).

NiLED1 Properties5.jpg

 

Here is the XAML for the VariablesForPLC object

<local:VariablesForPLC x:Key="VariablesForPLCDataSource" d:IsDataSource="True"/>

 

Settings in Blend to bind Value property of NiLED1 to Sta1Mode.  Yellow outline around Value property indicates a bound element.

NiLED1 Properties6.jpg

 

XAML for NiLED1

<ni:LED x:Name="NiLED1" HorizontalAlignment="Left" Height="23" Margin="191.973,373.934,0,0" VerticalAlignment="Top" Width="23" FalseBrush="#FF3A3A3C" TrueBrush="#FFC8D10A" Style="{DynamicResource LEDStyle1}" IsTabStop="False" Focusable="False" IsReadOnly="False" Value="{Binding Sta1Mode}">
                                	<ni:LED.DataContext>
                                		<local:VariablesForPLC/>
                                	</ni:LED.DataContext>
                                </ni:LED>

I will upload the solution file to Dropbox tonight, so let me know if that helps if you don't see anything here from what I posted.  Thanks for any help you can provide.  I'm sure the issue will be something silly on my end.  

0 Kudos
Message 6 of 8
(2,975 Views)

In the first screenshot, you are setting Sta1Mode on StationModes — where does the StationModes variable come from?

 

My best guess is that you are modifying one VariablesForPLC object, and the LED is referencing a different VariablesForPLC object. In the XAML, you listed both <local:VariablesForPLC x:Key="VariablesForPLCDataSource" d:IsDataSource="True"/> and <ni:LED.DataContext> <local:VariablesForPLC/> </ni:LED.DataContext>, which would create two separate object instances. For example, in the debugger you could check what NiLED1.DataContext.Sta1Mode looks like after btnModeSta1_Click runs.

~ Paul H
0 Kudos
Message 7 of 8
(2,970 Views)

I got it....finally.  There requires one line of code in the code behind to make it work. ---->  Datacontext = StationModes.  This makes the object (StationModes) available for databinding.  

0 Kudos
Message 8 of 8
(2,940 Views)