2 x Data Bindings in one label

Posts: 40
Hi. How can I switch 2 data bindings to my label?

I have a label with 2 data bindings (full name & team name) in my ticker, how can I change it with a button? greetings Jojo
Posts: 61
I'm not sure its possible to change data bindings inside the label itself. You might better of having 1 label with binding 1, and 1 label with binding 2, and switch them out with the button (eg. showing label 1 with the driver name, and after clicking the button, hide label 1, and show label 2 with the team name)
Posts: 785
There are several options. The one I like and use in the upcoming ATVO theme is to have multiple SubWidgets in your ticker, and exchange the TemplateSubWidget property in a script. You can have one template with fullname and another with team name. The downside of this is that it will rebuild and restart your ticker, so you cannot really do it "on the fly".

public class ChangeResultsMode : IScript
{
public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
{
// The name of the desired template is passed to the script as the value
var templateName = value?.ToString();
if (!string.IsNullOrWhiteSpace(templateName))
{
// Find the ticker and the desired template subwidget
var resultsTicker = item.Theme.Widgets.Find("W_ResultsTicker");
var template = resultsTicker.SubWidgets.Find(templateName);

// Change the template
if (template != null)
{
resultsTicker.Ticker.TemplateSubWidget = template;
}
else
{
Console.WriteLine("Invalid results ticker template subwidget name: " + templateName);
}
}
return null;
}
}



A similar option is to actually change the bindings. That example will also be in the ATVO theme. I'm pretty sure this also requires a ticker restart though, they won't update on the fly.

This example is a bit more complicated because I needed it to work on multiple widgets, hence some code to find the matching subwidgets and labels. The core logic is to get a binding by its key/name via "widget.DataSet.GetBindingByKey", then clear the existing list of bindings on the label and add the new one.

using System;
using ATVO.ThemesSDK;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Scripting.DotNET;
using ATVO.ThemeEditor.Data;
using System.Collections.Generic;
using System.Linq;

namespace Scripts
{
public class ChangeDriverInfoBottomBinding : IScript
{
public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
{
if (value is string)
{
var bindingKey = value?.ToString();
Console.WriteLine("Changing driver info binding to " + bindingKey);

var widgets = new List<Widget>();
widgets.Add(item.Theme.Widgets.Find("W_DriverInfo"));
widgets.Add(item.Theme.Widgets.Find("W_DriverInfoRight"));

foreach (var widget in widgets)
{
var binding = widget.DataSet.GetBindingByKey(bindingKey);
var subwidget = widget.SubWidgets.FirstOrDefault(sw => sw.Name.StartsWith("SW_DriverInfo"));
var label = subwidget?.Labels.FirstOrDefault(l => l.Name.StartsWith("L_DriverInfoBottom"));
if (label != null)
{
label.DataBindings.Clear();
if (binding != null)
{
label.DataBindings.Add(binding);
label.Text = "{0}";
}
else
{
label.Text = "";
}
}
else {
Console.WriteLine("Did not find label");
}

}
}
return null;
}
}
}


To update "on the fly" the best option is indeed to use a script converter to return the appropriate value depending on some condition.

The worst way is to have multiple labels and change their visibility in a script. The catch here is that you can't just do it on the template widget but you have to do it on the actual copies of each subwidget, so you'll have to loop over all subwidgets in the ticker. And it will reset whenever the ticker decides to rebuild which can be anytime. Not recommended.
Edited (1 time)