Exemplo n.º 1
0
        public static void ObjFieldBinding(System.Windows.Controls.Control control, DependencyProperty dependencyProperty, object obj, string property, BindingMode BindingMode = BindingMode.TwoWay)
        {
            //TODO: add Inotify on the obj.attr - so code changes to property will be reflected
            //TODO: check perf impact + reuse existing binding on same obj.prop
            try
            {
                Binding b = new Binding();
                b.Source = obj;
                b.Path   = new PropertyPath(property);
                b.Mode   = BindingMode;
                b.UpdateSourceTrigger     = UpdateSourceTrigger.PropertyChanged;
                b.NotifyOnValidationError = true;
                control.SetBinding(dependencyProperty, b);
            }
            catch (Exception ex)
            {
                //it is possible we load an old enum or something else which will cause the binding to fail
                // Can happen also if the bind field name is incorrect
                // mark the control in red, instead of not openning the Page
                // Set a tool tip with the error

                control.Style           = null; // remove style so red will show
                control.Background      = System.Windows.Media.Brushes.LightPink;
                control.BorderThickness = new Thickness(2);
                control.BorderBrush     = System.Windows.Media.Brushes.Red;

                control.ToolTip = "Error binding control to property: " + Environment.NewLine + property + " Please open a defect with all information,  " + Environment.NewLine + ex.Message;
            }
        }
Exemplo n.º 2
0
 public static void BindProperty(Control control, object source, string path,
     DependencyProperty property, BindingMode mode)
 {
     var binding = new Binding(path);
     binding.Source = source;
     binding.Mode = mode;
     control.SetBinding(property, binding);
 }
Exemplo n.º 3
0
        void ObjFieldBinding(System.Windows.Controls.Control control, DependencyProperty dependencyProperty, object obj, string property)
        {
            Binding b = new Binding();

            b.Source = obj;
            b.Path   = new PropertyPath(property);
            b.Mode   = BindingMode.TwoWay;
            b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            control.SetBinding(dependencyProperty, b);
        }
Exemplo n.º 4
0
        public static void LoadAndMonitor(INotifyPropertyChanged source, string sourceProperty, System.Windows.Controls.Control control, System.Windows.DependencyProperty controlProperty = null)
        {
            if (controlProperty == null)
            {
                if (control is System.Windows.Controls.TextBox)
                {
                    controlProperty = System.Windows.Controls.TextBox.TextProperty;
                }
                if (control is System.Windows.Controls.CheckBox)
                {
                    controlProperty = System.Windows.Controls.Primitives.ToggleButton.IsCheckedProperty;
                }
                if (control is System.Windows.Controls.ComboBox || control is System.Windows.Controls.ListBox)
                {
                    controlProperty = System.Windows.Controls.Primitives.Selector.SelectedValueProperty;
                }
            }
            var binding = new System.Windows.Data.Binding(sourceProperty);

            binding.Source  = source;
            binding.IsAsync = true;
            control.SetBinding(controlProperty, binding);
        }
 /// <summary>
 /// Sets the background binding.
 /// </summary>
 /// <param name="d">The cell definition.</param>
 /// <param name="c">The control.</param>
 protected virtual void SetBackgroundBinding(CellDefinition d, Control c)
 {
     if (d.BackgroundBindingPath != null)
     {
         var binding = new Binding(d.BackgroundBindingPath) { Source = d.BackgroundBindingSource };
         c.SetBinding(Control.BackgroundProperty, binding);
     }
 }
 public static void Bind(Control control, Action<Control> dataContextChanged)
 {
     control.SetBinding(InternalDataContextProperty, new Binding());
     control.SetValue(DataContextChangedProperty, dataContextChanged);
 }
        public void Attach(MetaAction metaAction, Control control)
        {
            ValidateType(control);

            var existingItems = controlAction.Where(it => it.Item1 == metaAction);
            var targetItem = existingItems.FirstOrDefault(it =>
            {
                Control c;
                if (it.Item2.TryGetTarget(out c))
                {
                    return c.Equals(control);
                }
                return false;
            });

            // already attached
            if (targetItem != null)
                return;
            Image image = null;
            if (metaAction.Icon != null)
            {
                image = ImageHelper.CreateImage(metaAction);
                image.MaxWidth = MetaAction.ToolBarIconSize;
                image.MaxHeight = MetaAction.ToolBarIconSize;
            }

            if (control is MenuItem)
            {
                var menu = (MenuItem)control;
                menu.Icon = image;
                menu.InputGestureText = metaAction.ShortCut;
                menu.Header = metaAction.Title;
                menu.ToolTip = metaAction.ToolTip;
            }
            if (control is ButtonBase)
            {
                var button = (ButtonBase)control;
                button.Content = image;
                button.ToolTip = metaAction.ToolTip;
                if (!string.IsNullOrEmpty(metaAction.ShortCut))
                {
                    button.ToolTip += ": " + metaAction.ShortCut;
                }
            }

            control.SetValue(ToolTipService.ShowOnDisabledProperty, true);
            // IsEnabled property

            // Visibility
            control.SetBinding(UIElement.VisibilityProperty,
                new Binding
                {
                    Source = metaAction,
                    Path = new PropertyPath("IsVisible"),
                    Mode = BindingMode.OneWay,
                    Converter = BooleanToVisibilityConverter.Instance,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                });

            // Shortcut
            if (metaAction.KeyBinding != null)
            {
                var window = GetWindow(control);
                if (!window.InputBindings.Contains(metaAction.KeyBinding))
                {
                    window.InputBindings.Add(metaAction.KeyBinding);
                }
            }
              //  Message.SetAttach(control, metaAction.CaliburnAction);
            control.SetValue(Message.AttachProperty, metaAction.CaliburnAction);
            controlAction.Add(new Tuple<MetaAction, WeakReference<Control>>(metaAction, new WeakReference<Control>(control)));
        }