簡體   English   中英

自定義控件條目未設置占位符顏色可綁定屬性

[英]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; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM