private void HookEvent() { string eventName = Event; if ((_attachedTo == null && _attachedTo2 == null) || Command == null || string.IsNullOrEmpty(eventName)) { return; } var attachedType = _attachedTo != null?_attachedTo.GetType() : _attachedTo2.GetType(); var eventToAttach = attachedType.GetEvent(eventName); if (eventToAttach != null) { var methodInfo = GetType().GetMethod("Execute", BindingFlags.Instance | BindingFlags.NonPublic); var del = Delegate.CreateDelegate(eventToAttach.EventHandlerType, this, methodInfo); if (_attachedTo != null) { eventToAttach.AddEventHandler(_attachedTo, del); } else { eventToAttach.AddEventHandler(_attachedTo2, del); } } else { throw new NullReferenceException("Command cannot be bound to event '" + eventName + "'. Event not found."); } }
public static void SetDefaultStyle(this FrameworkContentElement element, ResourceDescriptor descriptor) { if (FrameworkElementFindResourceMethod == null) { throw new InvalidOperationException("Invalid framework version"); } var resource = FrameworkElementFindResourceMethod.Invoke(null, new object[] { element, null, element.GetType() }); if (resource == DependencyProperty.UnsetValue) { element.Style = GetResource <Style>(descriptor); } }
private void GenerateContent(DataTemplate itemsPanel, DataTemplate itemTemplate, IEnumerable itemsSource) { Blocks.Clear(); if (itemTemplate != null && itemsSource != null) { FrameworkContentElement panel = null; foreach (object data in itemsSource) { if (panel == null) { if (itemsPanel == null) { panel = this; } else { FrameworkContentElement p = Helpers.LoadDataTemplate(itemsPanel); if (!(p is Block)) { throw new Exception("ItemsPanel must be a block element"); } Blocks.Add((Block)p); panel = Attached.GetItemsHost(p); if (panel == null) { throw new Exception("ItemsHost not found. Did you forget to specify Attached.IsItemsHost?"); } } } FrameworkContentElement element = Helpers.LoadDataTemplate(itemTemplate); element.DataContext = data; Helpers.UnFixupDataContext(element); if (panel is Section) { ((Section)panel).Blocks.Add(Helpers.ConvertToBlock(data, element)); } else if (panel is TableRowGroup) { ((TableRowGroup)panel).Rows.Add((TableRow)element); } else if (panel is List) { (panel as List).ListItems.Add((ListItem)element); } else { throw new Exception(String.Format("Don't know how to add an instance of {0} to an instance of {1}", element.GetType(), panel.GetType())); } } } }
/// <summary> /// Finds a BeginStoryboard with the given name, following the rules /// governing Storyboard. Returns null if not found. /// </summary> /// <remarks> /// If a name scope is given, look there and nowhere else. In the /// absense of name scope, use Framework(Content)Element.FindName which /// has its own complex set of rules for looking up name scopes. /// /// This is a different set of rules than from that used to look up /// the TargetName. BeginStoryboard name is registered with the template /// INameScope on a per-template basis. So we look it up using /// INameScope.FindName(). This is a function completely different from /// Template.FindName(). /// </remarks> internal static BeginStoryboard ResolveBeginStoryboardName( string targetName, INameScope nameScope, FrameworkElement fe, FrameworkContentElement fce) { object namedObject = null; BeginStoryboard beginStoryboard = null; if( nameScope != null ) { namedObject = nameScope.FindName(targetName); if( namedObject == null ) { throw new InvalidOperationException( SR.Get(SRID.Storyboard_NameNotFound, targetName, nameScope.GetType().ToString())); } } else if( fe != null ) { namedObject = fe.FindName(targetName); if( namedObject == null ) { throw new InvalidOperationException( SR.Get(SRID.Storyboard_NameNotFound, targetName, fe.GetType().ToString())); } } else if( fce != null ) { namedObject = fce.FindName(targetName); if( namedObject == null ) { throw new InvalidOperationException( SR.Get(SRID.Storyboard_NameNotFound, targetName, fce.GetType().ToString())); } } else { throw new InvalidOperationException( SR.Get(SRID.Storyboard_NoNameScope, targetName)); } beginStoryboard = namedObject as BeginStoryboard; if( beginStoryboard == null ) { throw new InvalidOperationException(SR.Get(SRID.Storyboard_BeginStoryboardNameNotFound, targetName)); } return beginStoryboard; }
/// <summary> /// Generates content based on the template information /// </summary> /// <exception cref="System.Exception">ItemsPanel must be a block element</exception> private void GenerateContent() { if (!IsLoaded) { return; } Blocks.Clear(); if ((ItemTemplate == null && ItemTemplateSelector == null) || ItemsSource == null) { return; } FrameworkContentElement panel = null; foreach (var data in ItemsSource) { if (panel == null) { if (ItemsPanel == null) { panel = this; } else { var itemsPanelTemplate = LoadDataTemplate(ItemsPanel); foreach (var templatePart in itemsPanelTemplate) { var block = templatePart as Block; if (block == null) { throw new Exception("ItemsPanel must be a block element"); } Blocks.Add(block); panel = DocEx.GetItemsHost(block); if (panel == null) { throw new Exception("ItemsHost not found. Did you forget to specify DocEx.IsItemsHost?"); } } } } var itemTemplate = ItemTemplateSelector != null?ItemTemplateSelector.SelectTemplate(data, this) : ItemTemplate; var elements = LoadDataTemplate(itemTemplate); foreach (var element in elements) { element.DataContext = data; var expressions = element.GetValue(DocEx.ForceRefreshExpressionProperty); if (expressions != null) { var bindings = expressions as List <BindingToSet>; if (bindings != null) { foreach (var binding in bindings) { if (binding.Binding.Source == null && binding.Binding.RelativeSource == null) { binding.Binding.Source = data; } var frameworkElement = binding.DependencyObject as FrameworkElement; if (frameworkElement != null) { frameworkElement.SetBinding(binding.Property, binding.Binding); } else { var frameworkContentElement = binding.DependencyObject as FrameworkContentElement; if (frameworkContentElement != null) { frameworkContentElement.SetBinding(binding.Property, binding.Binding); } } } } } var section = panel as Section; var paragraph = panel as Paragraph; var inline = element as Inline; var tableRowGroup = panel as TableRowGroup; var tableRow = element as TableRow; var list = panel as List; var listItem = element as ListItem; if (section != null) { section.Blocks.Add(ConvertToBlock(data, element)); } else if (paragraph != null && inline != null) { if (inline.Parent == null) { paragraph.Inlines.Add(inline); } } else if (tableRowGroup != null && tableRow != null) { if (tableRow.Parent == null) { tableRowGroup.Rows.Add(tableRow); } } else if (list != null && listItem != null) { if (listItem.Parent == null) { list.ListItems.Add(listItem); } } else if (panel != null) { throw new Exception(String.Format("Can't add an instance of {0} to an instance of {1}", element.GetType(), panel.GetType())); } else { throw new Exception("Unable to add child elements."); } } } }