0
這是我的示例代碼。Xamarin.Forms中的條目的背景顏色當禁用時iOS沒有更改
Entry entry= new Entry();
entry.BackgroundColor=Color.Teal;
entry.Enabled=false;
您可以看到條目背景顏色在禁用狀態下未更改。是否表示實際行爲?在這種情況下,一個如何區分進入控制的啓用和禁用狀態。
這是我的示例代碼。Xamarin.Forms中的條目的背景顏色當禁用時iOS沒有更改
Entry entry= new Entry();
entry.BackgroundColor=Color.Teal;
entry.Enabled=false;
您可以看到條目背景顏色在禁用狀態下未更改。是否表示實際行爲?在這種情況下,一個如何區分進入控制的啓用和禁用狀態。
這是默認的行爲。除非控件具有實際屬性,否則樣式禁用的項目不是開箱即用的。但你可以通過幾種不同的方式輕鬆實現這樣的事情。一種方法是創建一個定製的Entry
,其中包含一個名爲DisabledStyle
的可綁定屬性。然後,您可以爲禁用的Entry
字段設置自定義樣式。
選項1:自定義輸入
public class ExtendedEntry : Entry
{
private Style normalStyle;
public Style DisabledStyle
{
get { return (Style)GetValue(DisabledStyleProperty); }
set { SetValue(DisabledStyleProperty, value); }
}
public static readonly BindableProperty DisabledStyleProperty = BindableProperty.Create(nameof(DisabledStyle), typeof(Style), typeof(ExtendedEntry), null, BindingMode.TwoWay, null, (obj, oldValue, newValue) => { });
public ExtendedEntry()
{
normalStyle = this.Style;
this.PropertyChanged += ExtendedEntry_PropertyChanged;
}
private void ExtendedEntry_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IsEnabled) && this.DisabledStyle != null)
{
if (this.IsEnabled)
this.Style = normalStyle;
else
this.Style = DisabledStyle;
}
}
}
選項2:觸發
另一種選擇是使用觸發器:
<Entry Placeholder="enter name">
<Entry.Triggers>
<Trigger TargetType="Entry"
Property="IsEnabled" Value="True">
<Setter Property="BackgroundColor" Value="Yellow" />
</Trigger>
</Entry.Triggers>
</Entry>