Hi,
Is there any way how to change visibility of MenuTool that contains GalleryTool? I need to dynamically hide MenuTool if GalleryTool Items collection is empty.
Thanks
Hello gxclarke,
I was looking into your requirements and in order to hide the empty element I can suggest you remove the MenuTool form the Tools collection of the XamRibbonGroup that it belongs. Depending on your scenario you can use different events and approaches to get the XamRibbonGroup collection and to check if the containing GalleryTool is empty. For example you can try handle the Loaded event of the XamRibbonTabItem and iterate between all of the elements:
XamRibbonTabItem tabItem=(sender as XamRibbonTabItem);
for (int i = 0; i < tabItem.Groups.Count; i++)
for (int j = 0; j < (tabItem.Groups[i] as XamRibbonGroup).Tools.Count; j++)
if ((tabItem.Groups[i] as XamRibbonGroup).Tools[j] is MenuTool)
{
MenuTool mt= (tabItem.Groups[i] as XamRibbonGroup).Tools[j] as MenuTool;
foreach (RibbonToolBase tool in mt.Items)
GalleryTool gt = tool as GalleryTool;
if (gt!=null && (gt as GalleryTool).Groups.Count == 0)
(tabItem.Groups[i] as XamRibbonGroup).Tools.Remove(mt);
}
Please let me know if I misunderstood you in any way or if you have any additional questions on the matter.
I was further looking into this and in order to avoid the loops depending on your scneario you can also use the methods XamRibbon.FindToolByID() or FindGroupByID().
If I can assist you furhter on the matter, please do not hesitate to ask.
Hello Elena,
Sorry for the delay and thank you for your reply.
I have resolved this issue by creating a custom menu tool derived from the MenuTool.
Here is the code.
public class CustomMenuTool: MenuTool
/// <summary>
/// The <see cref="Visibility" /> dependency property's name.
/// </summary>
public const string VisibilityPropertyName = "Visibility";
/// Gets or sets the value of the <see cref="Visibility" />
/// property. This is a dependency property.
public Visibility Visibility
get
return (Visibility)GetValue(VisibilityProperty);
set
SetValue(VisibilityProperty, value);
/// Identifies the <see cref="Visibility" /> dependency property.
public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register(
VisibilityPropertyName,
typeof(Visibility),
typeof(CustomMenuTool),
new PropertyMetadata(Visibility.Collapsed, OnVisibilityChanged));
private static void OnVisibilityChangedDependencyObject d, DependencyPropertyChangedEventArgs e)
CustomMenuTool tool = (CustomMenuTool) d;
if (tool._toolControls!= null&& tool._toolControls.Count>0)
foreach (RibbonToolBaseControl toolControl in tool._toolControls)
toolControl.Visibility = (Visibility) e.NewValue;
private List<RibbonToolBaseControl> _toolControls = new List<RibbonToolBaseControl>(2);
protected override RibbonToolBaseControl ResolveToolControl()
var toolControl = base.ResolveToolControl();
toolControl.Visibility = Visibility;
_toolControls.Add(toolControl);
return toolControl;
Thank you,
Gary
I am just checking have you been able to resolve your issue? If you still need any assistance on the matter do not hesitate to ask.
In case the above suggestion helped you solve your issue please verify the thread as answered, so other users may take better advantage of it.