To download NI software, including the products shown below, visit ni.com/downloads.
Overview
This is a simple example that binds a button click from one button to the color property of another button using the WPF controls in Measurement Studio.
Description
This example is simple example that demonstrates how to bind an action, in this case a mouse click, to change a property on another contorl. In this example we are changing the case the color of another button. There are a few key pieces of code required to make this successful. One is in the XAML itself:
<Grid>
<ni:BooleanButton Content="Button 1" HorizontalAlignment="Left" Height="23" \
VerticalAlignment="Top" Width="75" Background="{Binding MyBrushProperty}" Margin="159,58,0,0"/>
<Button Content="Click to turn Button 1 Red" HorizontalAlignment="Left" \
VerticalAlignment="Top" Width="190" Margin="100,137,0,0" Click="Button_Click"/>
</Grid>
In this snippet, we can see that we are binding the background color of the BooleanButton to MyBrushProperty. This leads us to the second piece of code that is really nessicary in order to complete this functionality:
public DependencyProperty MyBrushProperty = DependencyProperty.Register(
"MyBrushProperty", typeof(SolidColorBrush), typeof(Window)
);
which is defined in our mainwindow class. This declares our databinding target as a DependencyProperty which is a component that is exposed in the CLR. Essentially it allows us to make changes to properties based on another set of inputs. This code allows us to expose the MyBrushProperty propert as a Depency Property so that we can perform bind data to it.
Requirements
Steps to Implement or Execute Code
Additional Information or References
Example what the the application looks like after click the button.
Related Links
MSDN: Dependency Properties Overview
MSDN: Dependency Property Class
MSDN Walkthrough: Getting Started with WPF
**This document has been updated to meet the current required format for the NI Code Exchange.**
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.