05-29-2018 02:01 PM
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
05-30-2018 08:06 AM
Hey busarider,
What environment are you using? In something like Visual Studio/Measurement Studio, you can manually bind data with objects.
Bdog
05-31-2018 03:44 AM
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
06-18-2018 09:27 AM - edited 06-18-2018 09:33 AM
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.
Setting databinding of LED value property to "Sta1Mode"
06-18-2018 11:29 AM
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.
06-18-2018 03:30 PM - edited 06-18-2018 03:33 PM
Click event of this button control sets the boolean property : Sta1Mode
T
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 (?).
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.
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.
06-18-2018 03:58 PM
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.
06-20-2018 09:27 AM - edited 06-20-2018 09:29 AM
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.