BrushValueEditor

Jun 8, 2011 at 8:10 AM

Hello,

At first, thank you so much for this control, it’s great!

Is there any implemented ValueEditorBase class for Bursh to working fine with Background … properties in SL40PropertyGrid.

I’m so appreciating to guide me to find it or give me idea to resolve it.

Thanks.

Coordinator
Jun 8, 2011 at 4:24 PM

Brush's are complicated because you can have different implementations (Solid, Radial, Linear, etc.). Steps to create an editor would be something like...

1. Determine the UserControl(s) that would support editing the different types of Brushs
2. Create those general user controls. (the hard part)
3. Using the StringEditor as a template create a BrushValueEditor, and using the CollectionEditor or ObjectEditor as an example for the Edit Button functionality
    A. Override the GetDisplayValue to return the string representation of the Brush Color and optionally the Type.
    B. When the edit button is clicked, evaluate the Type Of brush, and show your custom user control in a ChildWindow or Popup, passing along the current brush.
    C. The user makes the changes and clicks OK
    D. OnClose, pass back the edited value and update the BrushValueEditor

Due to the complexity of the specific brush user controls for each type of a brush, I have left this up to the individual.

Jul 18, 2011 at 12:01 PM

I alredy did it (probaly not in the best way, but works), here's my example, I hope it might help u

public class PWColorValueEditor : ValueEditorBase
    {
        PWColorPicker picker;
        TextBox txt;

        public PWColorValueEditor(PropertyGridLabel label, PropertyItem property)
            : base(label, property)
        {
            if (property.PropertyType == typeof(Char))
            {
                if ((char)property.Value == '\0')
                    property.Value = "";
            }

            property.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(property_PropertyChanged);
            property.ValueError += new EventHandler<ExceptionEventArgs>(property_ValueError);

           
            picker = new PWColorPicker();
            picker.Width = 105;
            picker.Visibility = System.Windows.Visibility.Collapsed;
            picker.VisibilityTextBlockCor = System.Windows.Visibility.Collapsed;
            picker.LostFocus += new RoutedEventHandler(picker_LostFocus);
            if (property.Value != null)
                picker.Cor = (SolidColorBrush)property.Value;

            if (this.Property.CanWrite)
                picker.OnCorChanged += new PWColorPicker.OnCorSelectionChangedHandler(picker_OnCorChanged);


            txt = new TextBox();
            txt.Height = 20;
            txt.BorderThickness = new Thickness(0);
            txt.Margin = new Thickness(0);
            txt.VerticalAlignment = VerticalAlignment.Center;
            txt.HorizontalAlignment = HorizontalAlignment.Stretch;
            txt.IsReadOnly = false;
            txt.SetBinding(TextBox.BackgroundProperty, new System.Windows.Data.Binding("Cor") { Source = picker });

            txt.GotFocus += new RoutedEventHandler(txt_GotFocus);


            StackPanel panel = new StackPanel();
            panel.Children.Add(txt);
            panel.Children.Add(picker);

            this.Content = panel;
            this.GotFocus += new RoutedEventHandler(StringValueEditor_GotFocus);
        }

        void picker_LostFocus(object sender, RoutedEventArgs e)
        {
            txt.Visibility = System.Windows.Visibility.Visible;
            picker.Visibility = System.Windows.Visibility.Collapsed;
        }

        void txt_GotFocus(object sender, RoutedEventArgs e)
        {
            txt.Visibility = System.Windows.Visibility.Collapsed;
            picker.Visibility = System.Windows.Visibility.Visible;
            picker.Focus();
        }

        void picker_OnCorChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.Property.CanWrite)
                this.Property.Value = (sender as PWColorPicker).Cor;

        }

        void property_ValueError(object sender, ExceptionEventArgs e)
        {
            MessageBox.Show(e.EventException.Message);
        }

        void property_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CanWrite")
            {
                if (!this.Property.CanWrite)
                    picker.OnCorChanged -= new PWColorPicker.OnCorSelectionChangedHandler(picker_OnCorChanged);
                else
                    picker.OnCorChanged += new PWColorPicker.OnCorSelectionChangedHandler(picker_OnCorChanged);

                picker.IsEnabled = !this.Property.CanWrite;
                //txt.Foreground = this.Property.CanWrite ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.Gray);
            }
        }

        void StringValueEditor_GotFocus(object sender, RoutedEventArgs e)
        {
            if(this.picker != null)
            this.picker.Focus();
        }

    }