WPF Implement editor placeholder (#8504)
authormelimion <33512073+melimion@users.noreply.github.com>
Fri, 22 Nov 2019 00:21:51 +0000 (03:21 +0300)
committerSamantha Houts <samhouts@users.noreply.github.com>
Fri, 22 Nov 2019 00:21:51 +0000 (16:21 -0800)
* Placeholder implemented

* PlaceholderColor added to EditorCoreGalleryPage

Xamarin.Forms.Controls/CoreGalleryPages/EditorCoreGalleryPage.cs
Xamarin.Forms.CustomAttributes/TestAttributes.cs
Xamarin.Forms.Platform.WPF/Renderers/EditorRenderer.cs

index 2f961e5..f4e977b 100644 (file)
@@ -21,6 +21,8 @@ namespace Xamarin.Forms.Controls
                        var textChangedContainer = new EventViewContainer<Editor>(Test.Editor.TextChanged, new Editor());
                        textChangedContainer.View.TextChanged += (sender, args) => textChangedContainer.EventFired();
                        var placeholderContainer = new ViewContainer<Editor>(Test.Editor.Placeholder, new Editor { Placeholder = "Placeholder" });
+
+                       var placeholderColorContainer = new ViewContainer<Editor>(Test.Editor.PlaceholderColor, new Editor { Placeholder = "I should have red placeholder", PlaceholderColor = Color.Red });
                        var textFontAttributesContainer = new ViewContainer<Editor>(Test.Editor.FontAttributes, new Editor { Text = "I have italic text", FontAttributes = FontAttributes.Italic });
                        var textFamilyContainer1 = new ViewContainer<Editor>(Test.Editor.FontFamily, new Editor { Text = "I have Comic Sans text in Win & Android", FontFamily = "Comic Sans MS" });
                        var textFamilyContainer2 = new ViewContainer<Editor>(Test.Editor.FontFamily, new Editor { Text = "I have bold Chalkboard text in iOS", FontFamily = "ChalkboardSE-Regular", FontAttributes = FontAttributes.Bold });
@@ -46,6 +48,7 @@ namespace Xamarin.Forms.Controls
                        Add(textContainer);
                        Add(textChangedContainer);
                        Add(placeholderContainer);
+                       Add(placeholderColorContainer);
                        Add(textFontAttributesContainer);
                        Add(textFamilyContainer1);
                        Add(textFamilyContainer2);
index a654629..0a100a1 100644 (file)
@@ -505,6 +505,7 @@ namespace Xamarin.Forms.CustomAttributes
                        Completed,
                        TextChanged,
                        Placeholder,
+                       PlaceholderColor,
                        Text,
                        TextColor,
                        FontAttributes,
index cbaef0a..efe21b6 100644 (file)
@@ -3,11 +3,13 @@ using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Media;
 using WpfScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility;
+using WControl = System.Windows.Controls.Control;
 
 namespace Xamarin.Forms.Platform.WPF
 {
-       public class EditorRenderer : ViewRenderer<Editor, TextBox>
+       public class EditorRenderer : ViewRenderer<Editor, FormsTextBox>
        {
+               Brush _placeholderDefaultBrush;
                bool _fontApplied;
 
                protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
@@ -16,15 +18,17 @@ namespace Xamarin.Forms.Platform.WPF
                        {
                                if (Control == null) // construct and SetNativeControl and suscribe control event
                                {
-                                       SetNativeControl(new TextBox { VerticalScrollBarVisibility = WpfScrollBarVisibility.Visible, TextWrapping = TextWrapping.Wrap, AcceptsReturn = true });
+                                       SetNativeControl(new FormsTextBox { VerticalScrollBarVisibility = WpfScrollBarVisibility.Visible, TextWrapping = TextWrapping.Wrap, AcceptsReturn = true });
                                        Control.LostFocus += NativeOnLostFocus; 
                                        Control.TextChanged += NativeOnTextChanged;
                                }
 
                                // Update control property 
                                UpdateText();
+                               UpdatePlaceholder();
                                UpdateInputScope();
                                UpdateTextColor();
+                               UpdatePlaceholderColor();
                                UpdateFont();
                                UpdateMaxLength();
                                UpdateIsReadOnly();
@@ -54,8 +58,42 @@ namespace Xamarin.Forms.Platform.WPF
                                UpdateMaxLength();
                        else if (e.PropertyName == InputView.IsReadOnlyProperty.PropertyName)
                                UpdateIsReadOnly();
+                       else if (e.PropertyName == Editor.PlaceholderProperty.PropertyName)
+                               UpdatePlaceholder();
+                       else if (e.PropertyName == Editor.PlaceholderColorProperty.PropertyName)
+                               UpdatePlaceholderColor();
                }
-               
+
+               void UpdatePlaceholder()
+               {
+                       Control.PlaceholderText = Element.Placeholder ?? string.Empty;
+               }
+
+               void UpdatePlaceholderColor()
+               {
+                       Color placeholderColor = Element.PlaceholderColor;
+
+                       if (placeholderColor.IsDefault)
+                       {
+                               if (_placeholderDefaultBrush == null)
+                               {
+                                       _placeholderDefaultBrush = (Brush)WControl.ForegroundProperty.GetMetadata(typeof(FormsTextBox)).DefaultValue;
+                               }
+
+                               // Use the cached default brush
+                               Control.PlaceholderForegroundBrush = _placeholderDefaultBrush;
+                               return;
+                       }
+
+                       if (_placeholderDefaultBrush == null)
+                       {
+                               // Cache the default brush in case we need to set the color back to default
+                               _placeholderDefaultBrush = Control.PlaceholderForegroundBrush;
+                       }
+
+                       Control.PlaceholderForegroundBrush = placeholderColor.ToBrush();
+               }
+
                void NativeOnTextChanged(object sender, System.Windows.Controls.TextChangedEventArgs textChangedEventArgs)
                {
                        ((IElementController)Element).SetValueFromRenderer(Editor.TextProperty, Control.Text);