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



我有一个自定义控件,它是一个日期选择器,但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. 当proerty值更改时,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 ;
    }
    

最新更新