public static bool SafeApplyTemplate(this FrameworkElement reference) { if (reference.CheckAccess()) { return(reference.ApplyTemplate()); } bool ret = false; reference.Dispatcher.Invoke(new Action(() => ret = reference.ApplyTemplate())); return(ret); }
public static Visual GetDescendantByName(Visual element, string name) { if (element == null) { return(null); } FrameworkElement elem = element as FrameworkElement; if (elem?.Name == name) { return(element); } Visual result = null; if (elem != null) { elem.ApplyTemplate(); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual visual = VisualTreeHelper.GetChild(element, i) as Visual; result = GetDescendantByName(visual, name); if (result != null) { break; } } return(result); }
static FrameworkElement GetDescendantByName(FrameworkElement element, string elementName) { if (element == null) { return(null); } if (element.Name == elementName) { return(element); } element.ApplyTemplate(); FrameworkElement match = null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { var child = VisualTreeHelper.GetChild(element, i) as FrameworkElement; match = GetDescendantByName(child, elementName); if (match != null) { break; } } return(match); }
/// <summary> /// Looks for a child control within a parent by type /// </summary> public static T FindChild <T>(this DependencyObject parent) where T : DependencyObject { // confirm parent is valid. if (parent == null) { return(null); } if (parent is T) { return(parent as T); } DependencyObject foundChild = null; if (parent is FrameworkElement) { FrameworkElement frameworkElement = parent as FrameworkElement; frameworkElement.ApplyTemplate(); } int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childrenCount; i++) { var child = VisualTreeHelper.GetChild(parent, i); foundChild = FindChild <T>(child); if (foundChild != null) { break; } } return(foundChild as T); }
/// <summary> /// Looks for a child control within a parent by name /// </summary> public static DependencyObject FindChild(this DependencyObject parent, string name) { // confirm parent and name are valid. if (parent == null || string.IsNullOrEmpty(name)) { return(null); } FrameworkElement frameworkElement = parent as FrameworkElement; if (frameworkElement != null && frameworkElement.Name == name) { return(parent); } DependencyObject result = null; frameworkElement?.ApplyTemplate(); int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childrenCount; i++) { var child = VisualTreeHelper.GetChild(parent, i); result = FindChild(child, name); if (result != null) { break; } } return(result); }
public static Visual GetDescendantByName(FrameworkElement element, string name) { if (element == null) { return(null); } if (element.Name == name) { return(element); } Visual descendantByName = null; if (element != null) { element.ApplyTemplate(); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual child = VisualTreeHelper.GetChild(element, i) as Visual; descendantByName = GetDescendantByName((FrameworkElement)child, name); if (descendantByName != null) { return(descendantByName); } } return(descendantByName); }
private static DesignerItem GetItemObjectControl(XAttribute typeAttribute)//根据属性类全名称创建控件的对象DesignerItem { string typeFullName = typeAttribute.Value; Type type = null; DesignerItem obj; var asm = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "Package\\Common.Package.dll"); type = asm.GetType(typeFullName.Split(',')[0]); //type = type.FastGetType(typeFullName); if (type == null) { return(null); } var fi = type.FastGetConstructor(); if (fi == null) { return(null); } try { obj = fi.Invoke() as DesignerItem; } catch { return(null); } FrameworkElement femt = obj as FrameworkElement; if (femt != null) { femt.ApplyTemplate(); } return(obj); }
public static Visual GetDescendantByType(Visual element, Type type) { if (element == null) { return(null); } if (element.GetType() == type) { return(element); } Visual foundElement = null; FrameworkElement elem = element as FrameworkElement; if (elem != null) { elem.ApplyTemplate(); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual visual = VisualTreeHelper.GetChild(element, i) as Visual; foundElement = GetDescendantByType(visual, type); if (foundElement != null) { break; } } return(foundElement); }
// Uses VisualChildren, as some elements are ommitted if logical chlidren are used private static void UpdateColorsVisual(this FrameworkElement element, object sender, ColorTheme theme) { foreach (Visual visual in GetVisualChildCollection <Visual>(element)) { visual.ApplyTheme(sender, theme); } element.ApplyTemplate(); }
protected ContentPresenter DiscoverContentControl(UIElement child) { FrameworkElement fe = child as FrameworkElement; if (fe != null) { if (fe is ContentPresenter) { return(fe as ContentPresenter); } try { fe.ApplyTemplate(); } catch { } int childrenCount = VisualTreeHelper.GetChildrenCount(fe); for (int i = 0; i < childrenCount; i++) { DependencyObject child1 = VisualTreeHelper.GetChild(child, i); ContentPresenter cp = DiscoverContentControl(child1 as UIElement); if (cp != null) { return(cp); } } } ContentControl c = child as ContentControl; if (c != null) { ContentPresenter cp = DiscoverContentControl(c.Content as UIElement); if (cp != null) { return(cp); } } ItemsControl ic = child as ItemsControl; if (ic != null) { foreach (Object elem in ic.Items) { if (elem is UIElement) { ContentPresenter cp = DiscoverContentControl(elem as UIElement); if (cp != null) { return(cp); } } } } return(null); }
public static void BindStyle(FrameworkElement element, string resourcePath) { string assemblyName = element.GetType().Assembly.FullName; try { ResourceDictionary resDict = new ResourceDictionary(); resDict.Source = new Uri(String.Format(@"pack://application:,,,/{0};component/{1}", assemblyName, resourcePath), UriKind.RelativeOrAbsolute); element.Resources.MergedDictionaries.Add(resDict); element.ApplyTemplate(); } catch (Exception ex) { Console.WriteLine("{0} BindStyle Failed! Uri: {1} of {2} Not found. \n{3}", element, resourcePath, assemblyName, ex.Message); } }
FrameworkElement LocateMainVisualization(FrameworkElement element) { if (null == element) { return(null); } while (null != element.Parent as FrameworkElement) { element = element.Parent as FrameworkElement; } element.ApplyTemplate(); return(element.FindName(MainVisualizationElementName) as FrameworkElement); }
private static bool ApplyTemplateImpl(FrameworkElement frameworkElement) { #if SILVERLIGHT var control = frameworkElement as Control; if (control != null) { return(control.ApplyTemplate()); } frameworkElement.Measure(XamlConstants.ZeroSize); frameworkElement.InvalidateMeasure(); return(true); #else return(frameworkElement.ApplyTemplate()); #endif }
/// <summary> /// Gets the object. /// </summary> /// <typeparam name="T">This is the type of control that is being searched for</typeparam> /// <param name="name">The name of the control that is being searched for</param> /// <returns>If the control is found a reference to this control is returned else null</returns> public static T GetObject <T>(string name) where T : class { if (_Viewbox != null && _Viewbox.Child != null) { FrameworkElement child = _Viewbox.Child as FrameworkElement; // we need to rebuild the visual tree since we are crafting xaml. child.ApplyTemplate(); // found the elements of a given tyme in the visual tree int count = VisualTreeHelper.GetChildrenCount(child); for (int i = 0; i < count; ++i) { var myname = VisualTreeHelper.GetChild(child, i); } } return(null); }
public void Render(Visual visual) { IPlatformRenderTargetBitmap impl = (IPlatformRenderTargetBitmap)this.PlatformImpl; FrameworkElement fe = visual as FrameworkElement; if (fe != null && !fe.IsInitialized) { fe.IsInitialized = true; fe.ApplyTemplate(); } using (DrawingContext context = impl.CreateDrawingContext()) { Renderer renderer = new Renderer(); renderer.Render(context, visual); } }
/// <summary> /// Hides the validation adorner. /// </summary> /// <param name="frameworkElement">The framework element.</param> public static void HideValidationAdorner(this FrameworkElement frameworkElement) { if (frameworkElement == null) { return; } #if NET frameworkElement.ApplyTemplate(); var adornerLayer = AdornerLayer.GetAdornerLayer(frameworkElement); if (adornerLayer != null) { adornerLayer.Visibility = Visibility.Collapsed; } Validation.SetValidationAdornerSite(frameworkElement, null); Validation.SetErrorTemplate(frameworkElement, null); #endif }
public void load(string player) { // get tiers filter List <string> tiers = auditTierComboBox.SelectedItemsOverride as List <string>; // if data loaded if (player != null && tabControl != null && tiers != null && Resources.Contains("AuditRecordsTemplate")) { DataGrid dataGrid = null; TabItem tabItem = tabControl.Items.Cast <TabItem>().FirstOrDefault(item => player.Equals(item.Header)); if (tabItem != null) { dataGrid = ((tabItem.Content as Grid).Children[0] as DataGrid); tabControl.SelectedItem = tabItem; } else { tabItem = new TabItem(); Grid grid = new Grid(); ControlTemplate template = (ControlTemplate)Resources["AuditRecordsTemplate"]; FrameworkElement elem = template.LoadContent() as FrameworkElement; if (elem != null) { elem.ApplyTemplate(); grid.Children.Add(elem); } tabItem.Content = grid; tabItem.Header = player; tabControl.Items.Add(tabItem); tabControl.SelectedItem = tabItem; dataGrid = (elem as DataGrid); } if (dataGrid != null) { dataGrid.ItemsSource = DataManager.getLootAudit(player, tiers, auditTimeSpinner.Value); } } }
protected List <UIElement> DiscoverNamedChildren(UIElement child) { List <UIElement> elements = new List <UIElement>(); FrameworkElement fe = child as FrameworkElement; if (fe != null) { if (!String.IsNullOrEmpty(((FrameworkElement)child).Name)) { elements.Add(child); } try { fe.ApplyTemplate(); } catch { } int childrenCount = VisualTreeHelper.GetChildrenCount(fe); for (int i = 0; i < childrenCount; i++) { DependencyObject child1 = VisualTreeHelper.GetChild(child, i); elements.AddRange(DiscoverNamedChildren(child1 as UIElement)); } } ContentControl c = child as ContentControl; if (c != null) { elements.AddRange(DiscoverNamedChildren(c.Content as UIElement)); } ItemsControl ic = child as ItemsControl; if (ic != null) { foreach (Object elem in ic.Items) { if (elem is UIElement) { elements.AddRange(DiscoverNamedChildren(elem as UIElement)); } } } return(elements); }
/// <summary> /// Calls a method when the Loaded event is fired on a FrameworkElement. /// </summary> /// <typeparam name="T">The type of the parameter to pass to the callback method.</typeparam> /// <param name="element">The element whose Loaded state we are interested in.</param> /// <param name="callback">The method we will call if element.IsLoaded is false.</param> /// <param name="parameter">The parameter to pass to the callback method.</param> /// <returns> /// Returns true if the element is not loaded and the callback will be called /// when the element is loaded, false otherwise. /// </returns> public static bool RetryActionAfterLoaded <T>(FrameworkElement element, RetryActionCallback <T> callback, T parameter) { if (element.IsLoaded) { return(false); } RetryActionAfterLoadedDataQueue data; if (!retryActionData.TryGetValue(element, out data)) { data = new RetryActionAfterLoadedDataQueue(); retryActionData.Add(element, data); } data.Enqueue(callback, parameter); element.Loaded += new RoutedEventHandler(Element_Loaded); element.ApplyTemplate(); return(true); }
/// <summary> /// Przechodzi przez drzewo wskazanego elementu i zwraca listę znalezionych potomków wskazanego typu. /// </summary> /// <param name="element">Element do przeszukania.</param> /// <param name="elements">Lista znalezionych potomków wskazanego typu.</param> /// <param name="level">Określa poziom przeszukiwania drzewa. Minus jeden (-1) oznacza pełne przeszukanie drzewa elementu.</param> private static void Descendants <T>(this FrameworkElement element, List <T> elements, int level) where T : FrameworkElement { if (element == null) { return; } element.ApplyTemplate(); T c = default(T); FrameworkElement child = null; int count = VisualTreeHelper.GetChildrenCount(element); for (int i = 0; i < count; i++) { child = VisualTreeHelper.GetChild(element, i) as FrameworkElement; if (child == null) { continue; } c = child as T; if (c != null) { elements.Add(c); } if (level < 0) { child.Descendants(elements, level); } else if (level > 0) { child.Descendants(elements, level - 1); } } }
private FrameworkElement FindElement(FrameworkElement parent, string name) { if (parent == null || string.IsNullOrEmpty(name)) { return(null); } if (parent.Name == name) { return(parent); } FrameworkElement result = null; parent.ApplyTemplate(); for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { FrameworkElement child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement; result = FindElement(child, name); if (result != null) { break; } } return(result); }
private static DependencyObject GetItemObject(XAttribute typeAttribute)//根据属性类全名称创建类的对象DepedencyObject { string typeFullName = typeAttribute.Value; Type type = null; DependencyObject obj; //if (typeFullName.Contains("Common.Package")) //{ // var asm = Assembly.LoadFile(@"E:\ECMS\trunk\Development\TestDesigner\bin\Debug\Package\Common.Package.dll"); // type = asm.GetType(typeFullName.Split(',')[0]); //} //else type = type.FastGetType(typeFullName); if (type == null) { return(null); } var fi = type.FastGetConstructor(); if (fi == null) { return(null); } try { obj = fi.Invoke() as DependencyObject; } catch { return(null); } FrameworkElement femt = obj as FrameworkElement; if (femt != null) { femt.ApplyTemplate(); } return(obj); #region //var typeFullName = typeAttribute.Value; //Type type = null; //type = Type.GetType(typeFullName);// ReSharper disable once ExpressionIsAlwaysNull //if (type == null) return null; ////IObjcet o = (IObjcet)TypeHelper.CreateObject<object>(); //var fi = type.GetConstructor(new Type[] { }); //DependencyObject obj; //if (fi == null) //{ // return null; //} //try //{ // //obj = fi[0].Invoke(BindingFlags.Default,null) as DependencyObject; // obj = fi.Invoke(new object[] { }) as DependencyObject; //} //catch(Exception ex) //{ // return null; //} //FrameworkElement frameworkElement = obj as FrameworkElement; //if (frameworkElement != null) //{ // frameworkElement.ApplyTemplate(); //} //return obj; #endregion }