Reputation: 1
This is my sample code.
Entry entry= new Entry();
entry.BackgroundColor=Color.Teal;
entry.Enabled=false;
You can see entry background color is not changed in disabled state.Is that actual behaviour? In that case one how can differentiate Enable and Disable state of entry control.
Upvotes: 0
Views: 1396
Reputation: 2981
This is default behaviour. Styling disabled items is not something that is supported out of the box unless the control has an actual property for it. but you can easily implement something like this in a few different ways. One would be to create a custom Entry
with a bindable property called something like DisabledStyle
. You can then set a custom style for disabled Entry
fields.
Option 1: Custom Entry
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;
}
}
}
Option 2: Triggers
Another option would be to use a trigger:
<Entry Placeholder="enter name">
<Entry.Triggers>
<Trigger TargetType="Entry"
Property="IsEnabled" Value="True">
<Setter Property="BackgroundColor" Value="Yellow" />
</Trigger>
</Entry.Triggers>
</Entry>
Upvotes: 1