Extracting array element in Live Binding: Difference between revisions
From OPC Labs Knowledge Base
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Live Binding]] | [[Category:Live Binding]] [[Category:Example]] {{Note|This article is largely obsolete, because newer versions of QuickOPC can provide the same (and better) functionality using the Element Extraction feature.}} | ||
Sometimes there is a requirement to display just one element of an array value provided by an OPC server. It is easy to take an element of an array in code, but currently (in QuickOPC version 2016.2) there is no built-in way to do it with Live Binding. | Sometimes there is a requirement to display just one element of an array value provided by an OPC server. It is easy to take an element of an array in code, but currently (in QuickOPC version 2016.2) there is no built-in way to do it with Live Binding. | ||
Latest revision as of 18:21, 8 February 2018
Note: This article is largely obsolete, because newer versions of QuickOPC can provide the same (and better) functionality using the Element Extraction feature.
Sometimes there is a requirement to display just one element of an array value provided by an OPC server. It is easy to take an element of an array in code, but currently (in QuickOPC version 2016.2) there is no built-in way to do it with Live Binding.
It is, however, very easy to write a data converter component that extracts the array element. The code has just several lines:
using System;
using System.ComponentModel;
using System.Globalization;
using OpcLabs.BaseLib.Data;
namespace WindowsFormsApplication1
{
public sealed class ArrayElementConverter : Component, IDataConverter
{
public ArrayElementConverter()
{
}
public ArrayElementConverter(IContainer container)
{
container.Add(this);
}
public object Convert(object value, object parameter, CultureInfo culture)
{
var array = (Array) value;
int index = int.Parse((string) parameter);
object result = array.GetValue(index);
return result;
}
public object ConvertBack(object value, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Inverse conversion is not supported by ArrayElementConverter.");
}
}
}
Note that the code is mainly for illustration purposes; a "production quality" code would probably contain better error handling and some other features.
Instructions:
- Create Live Binding project in the usual way.
- Add an ArrayElementConverter class with the code above (possibly changing the namespace).
- Build the project.
- Open the form which should contain the binding for an array element.
- Drag the ArrayElementConverter component from the Toolbox to the design area of the form.
- Add the binding you need. For now, the binding will bind to the whole array.
- Select the control with the binding, and perform "Edit Live Bindings".
- In Binding Collection Editor, select the binding.
- In the property grid, under Value Conversion group, click on the Converter property, click the drop-down button on the right, and select the arrayElementConverter1 component.
- In Converter Parameter property row, enter the index of the array element you want to extract. This has to be a valid integer that falls into the array bounds.
- Press OK to close the Binding Collection Editor.
- Build the project and test it.