自定义控件条目未设置占位符颜色可绑定 属性

Custom Control Entry not getting set the Place Holder Color Bindable Property

我有一个自定义控件,它是一个日期选择器,但 PlaceHolderColor 未设置且没有影响。

public Color PlaceHolderColorText
{
     get => 
     (Color)GetValue(PlaceHolderColorProperty);
     set => SetValue(PlaceHolderColorProperty,
     value);
}

 public static readonly BindableProperty
 PlaceHolderColorProperty = 
BindableProperty.Create(nameof(PlaceHolderColor), 
typeof(Color), 
typeof(DateTimePicker2),Color.Black , BindingMode.TwoWay, 
 propertyChanged: OnPlaceHodlerColorChanged);

我正在创建动态条目

public Entry _dayEntry { get; private set; } = new Entry() { 
TabIndex = 0, Placeholder = "dd", Keyboard = Keyboard.Numeric, 
WidthRequest = 60, HorizontalOptions = LayoutOptions.Start };
 
public Entry _monthEntry { get; private set; } = new Entry() { 
TabIndex = 1, Placeholder = "MM", Keyboard = Keyboard.Numeric, 
WidthRequest = 60, HorizontalOptions = LayoutOptions.Start };
  

但我不确定在这里我需要什么来获得正确应用于条目的值

public static void OnPlaceHodlerColorChanged(BindableObject 
bindable, object oldValue, object newValue)
{
        var test = newValue;      
     ///do something with new but dont no what.
}

我正在将这样的条目添加到堆栈的子项中

public DateTimePicker2()
{
    BindingContext = this;

    _dayEntry.TextChanged += _dayEntry_TextChanged;
    _monthEntry.TextChanged += _monthEntry_TextChanged;
    _yearEntry.TextChanged += _yearEntry_TextChanged;
    _dayEntry.TextColor= TextColor;
    _monthEntry.TextColor= TextColor;
    _yearEntry.TextColor= TextColor;
    _dayEntry.PlaceholderColor = PlaceHolderColor;
    _monthEntry.PlaceholderColor = PlaceHolderColor;
    _yearEntry.PlaceholderColor = PlaceHolderColor;
    _hourEntry.TextColor= TextColor;
    _minsEntry.TextColor= TextColor;
    SelectedDateChanged +=DateTimePicker2_SelectedDateChanged;
    Content = new StackLayout()
    {
        Orientation = StackOrientation.Horizontal,
            Children =
            {    _datePicker,
                 _timePicker,
                _monthEntry,
                _dayEntry,                    
                _yearEntry,
                _hourEntry,
                _minsEntry,
                _ampmPicker,
                iconButton

 }
};

对于如何让事件注册占位符颜色以更正您将在此处看到的条目,我有点困惑。

_dayEntry.PlaceholderColor = PlaceHolderColor;

提前致谢

  1. 初始BindableProperty正确,名称应与属性相同。

    public Color PlaceHolderColor
    {
         get => 
      (Color)GetValue(PlaceHolderColorProperty);
         set => SetValue(PlaceHolderColorProperty,
      value);
    }
    
    public static readonly BindableProperty PlaceHolderColorProperty = 
    BindableProperty.Create(nameof(PlaceHolderColor), 
    typeof(Color), 
    typeof(DateTimePicker2),Color.Black , BindingMode.TwoWay, 
    propertyChanged: OnPlaceHodlerColorChanged);
    
  2. OnPlaceHodlerColorChanged 事件在属性值更改时触发,因此您需要在 Entry.PlaceholderColor 上手动设置它。

    public static void OnPlaceHodlerColorChanged(BindableObject bindable, object oldValue, object newValue)
    {
         Color test = newValue as Color;      
    
         var control = (DateTimePicker2)bindable;
         control._dayEntry.PlaceholderColor = test ;
         control._monthEntry.PlaceholderColor = test ;
         control._yearEntry.PlaceholderColor = test ;
    }