Exemple #1
0
            public Point?GetElementPosition(IViewAware element, IViewAware relativeTo)
            {
                if (element.GetView() == null)
                {
                    return(null);
                }

                return(((UIElement)element.GetView()).TranslatePoint(new Point(), (UIElement)relativeTo.GetView()));
            }
Exemple #2
0
            public IObservable <Unit> ViewLoaded(IViewAware element)
            {
                IObservable <Unit> getView = element.GetView() != null?Observable.Return(Unit.Default) :
                                                 Observable.FromEventPattern(element, "ViewAttached").Select(_ => Unit.Default).Take(1);

                return
                    (from gotView in getView
                     from loaded in Observable.FromEventPattern(element.GetView(), "Loaded").Take(1).Concat(Observable.FromEventPattern(element.GetView(), "SizeChanged"))
                     select Unit.Default);
            }
Exemple #3
0
            public void ShowWindow(IViewAware rootModel, IViewAware owningModel)
            {
                Window win = CreateWindow(rootModel, isDialog: false, context: null, settings: null);

                win.Owner = (Window)owningModel.GetView();
                win.Show();
            }
Exemple #4
0
        public static bool SetFocus(this IViewAware screen, string property)
        {
            var view = screen.GetView() as UserControl;

            if (view != null)
            {
                var  control = FindChild(view, property);
                bool focus   = control != null && control.Focus();
                return(focus);
            }
            return(false);
        }
Exemple #5
0
        public static bool SetFocus(this IViewAware screen, string property)
        {
            Contract.Requires(property != null, "Property cannot be null.");
            if (!(screen.GetView() is UserControl view))
            {
                return(false);
            }
            var  control = FindChild(view, property);
            bool focus   = control != null && control.Focus();

            return(focus);
        }
        /// <summary>
        /// Usage from a screen: e.g. control with xName="MyTextBox" -> this.SetFocus("MyTextBox"); <para />
        /// If setting focus during initial screen load, this must be called from "OnViewLoaded".
        /// </summary>
        public static bool SetFocus(this IViewAware screen, string property)
        {
            Contract.Requires(property != null, "Property cannot be null.");
            var view = screen.GetView() as ContentControl;

            if (view != null)
            {
                var  control = view.FindChild(property);
                bool focus   = control != null && control.Focus();
                return(focus);
            }
            return(false);
        }
Exemple #7
0
        public static void SetFocus(this IViewAware screen, string property)
        {
            if (!(screen.GetView() is DependencyObject view))
            {
                return;
            }

            FrameworkElement control = FindChild(view, property);

            if (control == null)
            {
                return;
            }

            KeyboardNavigationEx.Focus(control);
        }
Exemple #8
0
    ModernFrame FindFrame(IViewAware viewAware)
    {
        // Get the view for the window
        var view = viewAware.GetView() as Control;

        if (view != null)
        {
            // Find the frame by name in the template
            var frame = view.Template.FindName("ContentFrame", view) as ModernFrame;
            if (frame != null)
            {
                return(frame);
            }
        }
        return(null);
    }
        private static MetroWindow GetMetroWindowFromScreen(this IViewAware screen)
        {
            var uiElement = screen?.GetView() as DependencyObject;

            if (uiElement == null)
            {
                throw new InvalidOperationException($"The view for {typeof(Screen).Name} is not a dependency object");
            }

            var metroWindow = MetroWindow.GetWindow(uiElement) as MetroWindow;

            if (metroWindow == null)
            {
                throw new InvalidOperationException($"Unable to get metro window from screen '{typeof(Screen).Name}'");
            }

            return(metroWindow);
        }
        public static void SetCaretIndexToEnd(this IViewAware screen, string property)
        {
            if (!(screen.GetView() is DependencyObject view))
            {
                return;
            }

            FrameworkElement control = VisualTreeExtensions.FindChild(view, property);

            if (control == null)
            {
                return;
            }

            TextBox textBox = VisualTreeExtensions.FindChild <TextBox>(control);

            if (textBox == null)
            {
                return;
            }

            textBox.CaretIndex = textBox.Text.Length;
        }
Exemple #11
0
 public Size GetElementSize(IViewAware element)
 {
     return(((UIElement)element.GetView()).RenderSize);
 }
Exemple #12
0
 public Point GetDropPosition(DragEventArgs e, IViewAware relativeTo)
 {
     return(e.GetPosition((IInputElement)relativeTo.GetView()));
 }
Exemple #13
0
 public Point GetMousePosition(MouseEventArgs e, IViewAware relativeTo)
 {
     return(e.GetPosition((IInputElement)relativeTo.GetView()));
 }
Exemple #14
0
 public Point GetPosition(IViewAware relativeTo)
 {
     return(e.GetPosition((IInputElement)relativeTo.GetView()));
 }
        /// <summary>
        /// Static method to apply labels for a model's properties.  The model specified MUST be view aware.
        /// </summary>
        /// <param name="model">The view to which the label should be applied</param>
        public static void ApplyLabels(IScreen model)
        {
            if (model == null)
            {
                return;
            }

            IViewAware va = model as IViewAware;

            if (va == null)
            {
                return;
            }

            object view = va.GetView();

            if (view == null)
            {
                return;
            }

            // Create a dictionary of the label (if any) associated with each class property
            Dictionary <string, LabelDescriptionAttribute[]> modelsLabels =
                (from p in model.GetType().GetProperties()
                 let attrs = (LabelDescriptionAttribute[])p.GetAttributes <LabelDescriptionAttribute>(true).ToArray()
                             where attrs.Length != 0
                             select new System.Collections.Generic.KeyValuePair <string, LabelDescriptionAttribute[]>(p.Name, attrs)
                ).ToDictionary(p => p.Key, p => p.Value);

            // Grab any model level properties
            var modelAttrs = model.GetType().GetAttributes <LabelDescriptionAttribute>(true).ToArray();

            if (modelAttrs.Count() > 0)
            {
                modelsLabels.Add("", modelAttrs);
            }

            // Grab the dictionary
            foreach (string name in modelsLabels.Keys)
            {
                foreach (LabelDescriptionAttribute l in modelsLabels[name])
                {
                    string label = "";

                    // Used to hold previously created classes
                    Dictionary <string, object> references = new Dictionary <string, object>();

                    if (l.LabelResourceType == null || string.IsNullOrEmpty(l.LabelResourceName))
                    {
                        label = l.Label;
                    }
                    else
                    {
                        // Look up the description (if possible)
                        object reference = null;

                        if (references.ContainsKey(l.LabelResourceType.Name))
                        {
                            reference = references[l.LabelResourceType.Name];
                        }
                        else
                        {
                            // Try to get the type
                            reference = Activator.CreateInstance(l.LabelResourceType, true);
                            references.Add(l.LabelResourceType.Name, reference);
                        }

                        // Now try to find the description in the reference class
                        PropertyInfo pi = reference.GetType().GetProperty(l.LabelResourceName);
                        if (pi == null)
                        {
                            continue;
                        }

                        object v = pi.GetValue(reference, null);
                        label = v.ToString();
                    }

                    // If this is a model level label then the attribute MUST specify an element name
                    if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(l.ElementName))
                    {
                        continue;
                    }

                    string labelName = string.IsNullOrEmpty(l.ElementName) ? name + "Label" : l.ElementName;

                    // Next, find the named element in the view
                    // Named elements appear as properties of the view
                    object element = (view as FrameworkElement).FindName(labelName);
                    if (element == null)
                    {
                        continue;
                    }

                    string labelPropertyName = string.IsNullOrEmpty(l.LabelPropertyName)
                                                ? element.GetType().Name == "Button"
                                                        ? "Content"
                                                        : element.GetType().Name == "TabItem"
                                                                ? "Header"
                                                                : "Text"
                                                : l.LabelPropertyName;

                    PropertyInfo piText = element.GetType().GetProperty(labelPropertyName);
                    if (piText == null)
                    {
                        continue;
                    }
                    piText.SetValue(element, label, null);
                }
            }
        }