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 });
Add(textContainer);
Add(textChangedContainer);
Add(placeholderContainer);
+ Add(placeholderColorContainer);
Add(textFontAttributesContainer);
Add(textFamilyContainer1);
Add(textFamilyContainer2);
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)
{
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();
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);