private List <BoundControl> FindBoundControls(string id, int?hwnd, RibbonBindingType type, object sourceObject = null) { List <BoundControl> erg = new List <BoundControl>(); try { if (!hwnd.HasValue) { hwnd = -1; } foreach (var boundControl in boundVMControls) { if (boundControl.BindingInfo.ID == id && (boundControl.Hwnd == hwnd || boundControl.Hwnd == -1) && boundControl.BindingInfo.RibbonBindingType == type && (sourceObject == null || boundControl.Binding.SourceObject == sourceObject)) { erg.Add(boundControl); } } } catch (Exception ex) { logger.Error(ex); } return(erg); }
private List <BoundControl> GetBoundItems(IRibbonControl control, int index, RibbonBindingType bindingType) { List <BoundControl> items = new List <BoundControl>(); try { object itemToFind = null; var boundLists = FindBoundControls(control.Id, control.GetHwnd(), RibbonBindingType.GalleryItemsSource); foreach (var boundList in boundLists) { if (boundList.Binding.Value is System.Collections.IList list) { itemToFind = list[index]; } } if (itemToFind != null) { foreach (var item in boundVMControls) { if (item.Binding.SourceObject == itemToFind && item.BindingInfo.RibbonBindingType == bindingType) { items.Add(item); } } } } catch (Exception ex) { logger.Error(ex); } return(items); }
private T GetItemBinding <T>(IRibbonControl control, int index, RibbonBindingType bindingType) { try { var ctrls = GetBoundItems(control, index, bindingType); foreach (var ctrl in ctrls) { if (ctrl.Binding.Value is T value) { return(value); } } } catch (Exception ex) { logger.Error(ex); } return(default(T)); }
private bool GetBoolBinding(IRibbonControl control, RibbonBindingType bindingType) { try { var ctrls = FindBoundControls(control.Id, control.GetHwnd(), bindingType); foreach (var ctrl in ctrls) { if (ctrl.Binding.Value is bool boolvalue) { return(boolvalue); } } } catch (Exception ex) { logger.Error(ex); } return(false); }
private static List <BindingInfo> ReplaceBindingAndExtractBindingInfo(XElement ribbonElement, string controlType, string attributeName, string newAttributeValue, RibbonBindingType bindingType, string parentID = null) { List <BindingInfo> bindingInfos = new List <BindingInfo>(); var elements = ribbonElement.DescendantsAndSelf().Where(ele => ele.Name.LocalName == controlType || controlType == "").ToList(); foreach (var element in elements) { var attr = element.Attributes().FirstOrDefault(atr => atr.Name.LocalName == attributeName); if (attr != null) { BindingInfo newBindingInfo = null; if (attr.Value.StartsWith("{Binding")) { newBindingInfo = new BindingInfo() { BindingPath = attr.Value.Replace("{Binding", "").Replace("}", "").Trim(), RibbonBindingType = bindingType, ID = element.Attributes().FirstOrDefault(atr => atr.Name.LocalName == "id")?.Value ?? "", ParentID = parentID }; bindingInfos.Add(newBindingInfo); if (newAttributeValue == "#remove") { attr.Remove(); } else { attr.Value = newAttributeValue; } if (controlType == "gallery" && newBindingInfo != null && attributeName == "itemssource") { newBindingInfo.SubInfos.AddRange(ReplaceBindingAndExtractBindingInfo(element, "gallery", "getItemID", nameof(CustomUI.GetItemID), RibbonBindingType.ItemId, newBindingInfo.ID)); newBindingInfo.SubInfos.AddRange(ReplaceBindingAndExtractBindingInfo(element, "gallery", "getItemLabel", nameof(CustomUI.GetItemLabel), RibbonBindingType.ItemLabel, newBindingInfo.ID)); newBindingInfo.SubInfos.AddRange(ReplaceBindingAndExtractBindingInfo(element, "gallery", "getItemImage", nameof(CustomUI.GetItemImage), RibbonBindingType.ItemImage, newBindingInfo.ID)); } } } } return(bindingInfos); }