Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active January 14, 2020 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/186cbc9c2a946dddc9eb83bc643f26b8 to your computer and use it in GitHub Desktop.
Save bjoerntx/186cbc9c2a946dddc9eb83bc643f26b8 to your computer and use it in GitHub Desktop.
private void ApplyMaskedString(RibbonItemCollection ribbonItems, dynamic data)
{
// flatten data object in case of an array
if (data.GetType() == typeof(JArray))
data = data[0];
// loop through all ribbon items in the "insert merge fields" menu
foreach (Control ribbonButton in ribbonItems)
{
// in case, the item is a drop-down, call ApplyMaskedString
// recursively with a new data object
if (ribbonButton is RibbonMenuButton)
{
ApplyMaskedString(
((RibbonMenuButton)ribbonButton).DropDownItems,
data[ribbonButton.Text]);
}
// in case it is a merge field insert button
else if (ribbonButton is RibbonButton)
{
// and it is not a separator or title
if (!ribbonButton.Name.StartsWith("TXITEM_"))
{
// get the data from the first data row
var dataValue = data[ribbonButton.Text];
if (dataValue == null)
continue;
if (dataValue.GetType() == typeof(JValue))
{
// change the actual text
ribbonButton.Text = dataValue.Value;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment