From: melimion <33512073+melimion@users.noreply.github.com> Date: Fri, 22 Nov 2019 00:21:51 +0000 (+0300) Subject: WPF Implement editor placeholder (#8504) X-Git-Tag: accepted/tizen/5.5/unified/20200421.150457~80 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b1fb98ebb11690b6e76d19c8ab955526260771f9;p=platform%2Fcore%2Fcsapi%2Fxsf.git WPF Implement editor placeholder (#8504) * Placeholder implemented * PlaceholderColor added to EditorCoreGalleryPage --- diff --git a/Xamarin.Forms.Controls/CoreGalleryPages/EditorCoreGalleryPage.cs b/Xamarin.Forms.Controls/CoreGalleryPages/EditorCoreGalleryPage.cs index 2f961e5..f4e977b 100644 --- a/Xamarin.Forms.Controls/CoreGalleryPages/EditorCoreGalleryPage.cs +++ b/Xamarin.Forms.Controls/CoreGalleryPages/EditorCoreGalleryPage.cs @@ -21,6 +21,8 @@ namespace Xamarin.Forms.Controls var textChangedContainer = new EventViewContainer(Test.Editor.TextChanged, new Editor()); textChangedContainer.View.TextChanged += (sender, args) => textChangedContainer.EventFired(); var placeholderContainer = new ViewContainer(Test.Editor.Placeholder, new Editor { Placeholder = "Placeholder" }); + + var placeholderColorContainer = new ViewContainer(Test.Editor.PlaceholderColor, new Editor { Placeholder = "I should have red placeholder", PlaceholderColor = Color.Red }); var textFontAttributesContainer = new ViewContainer(Test.Editor.FontAttributes, new Editor { Text = "I have italic text", FontAttributes = FontAttributes.Italic }); var textFamilyContainer1 = new ViewContainer(Test.Editor.FontFamily, new Editor { Text = "I have Comic Sans text in Win & Android", FontFamily = "Comic Sans MS" }); var textFamilyContainer2 = new ViewContainer(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); diff --git a/Xamarin.Forms.CustomAttributes/TestAttributes.cs b/Xamarin.Forms.CustomAttributes/TestAttributes.cs index a654629..0a100a1 100644 --- a/Xamarin.Forms.CustomAttributes/TestAttributes.cs +++ b/Xamarin.Forms.CustomAttributes/TestAttributes.cs @@ -505,6 +505,7 @@ namespace Xamarin.Forms.CustomAttributes Completed, TextChanged, Placeholder, + PlaceholderColor, Text, TextColor, FontAttributes, diff --git a/Xamarin.Forms.Platform.WPF/Renderers/EditorRenderer.cs b/Xamarin.Forms.Platform.WPF/Renderers/EditorRenderer.cs index cbaef0a..efe21b6 100644 --- a/Xamarin.Forms.Platform.WPF/Renderers/EditorRenderer.cs +++ b/Xamarin.Forms.Platform.WPF/Renderers/EditorRenderer.cs @@ -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 + public class EditorRenderer : ViewRenderer { + Brush _placeholderDefaultBrush; bool _fontApplied; protected override void OnElementChanged(ElementChangedEventArgs 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);