I am trying to make a simple control that combines an entry and a label.
The binding doesn't work, however, and I don't understand why.
Can anyone help clarify?
Here's my control xaml
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Custom.Controls.LabelEntry">
<ContentView.Content>
<Grid x:Name="cGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label x:Name="cLabel"
Grid.Column="0"/>
<Entry x:Name="cEntry"
Grid.Column="1"/>
</Grid>
</ContentView.Content>
</ContentView>
and my control xaml.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LabelEntry : ContentView
{
public LabelEntry()
{
InitializeComponent();
}
#region Binding for LabelText
public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(
propertyName: "LabelText",
returnType: typeof(string),
declaringType: typeof(LabelEntry),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: LabelTextPropertyChanged);
public string LabelText { get; set; }
private static void LabelTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (LabelEntry)bindable;
control.cLabel.Text = newValue?.ToString();
}
#endregion
#region Binding for LabelStyle
public static readonly BindableProperty LabelStyleProperty = BindableProperty.Create(
propertyName: "LabelStyle",
returnType: typeof(Style),
declaringType: typeof(LabelEntry),
defaultValue: null,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: LabelStylePropertyChanged);
public Style LabelStyle { get; set; }
private static void LabelStylePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (LabelEntry)bindable;
control.cLabel.Style = (Style)newValue;
}
#endregion
#region Binding for EntryText
public static readonly BindableProperty EntryTextProperty = BindableProperty.Create(
propertyName: "EntryText",
returnType: typeof(string),
declaringType: typeof(LabelEntry),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: EntryTextPropertyChanged
);
public string EntryText
{
get => (string)GetValue(EntryTextProperty);
set => SetValue(EntryTextProperty, value);
}
private static void EntryTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (LabelEntry)bindable;
control.cEntry.Text = newValue?.ToString();
}
#endregion
#region Binding for EntryPlaceholder
public static readonly BindableProperty EntryPlaceholderProperty = BindableProperty.Create(
propertyName: "EntryPlaceholder",
returnType: typeof(string),
declaringType: typeof(LabelEntry),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: EntryPlaceholderPropertyChanged);
public string EntryPlaceholder { get; set; }
private static void EntryPlaceholderPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (LabelEntry)bindable;
control.cEntry.Placeholder = newValue?.ToString();
}
#endregion
#region Binding for EntryStyle
public static readonly BindableProperty EntryStyleProperty = BindableProperty.Create(
propertyName: "EntryStyle",
returnType: typeof(Style),
declaringType: typeof(LabelEntry),
defaultValue: null,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: EntryStylePropertyChanged);
public Style EntryStyle { get; set; }
private static void EntryStylePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (LabelEntry)bindable;
control.cEntry.Style = (Style)newValue;
}
#endregion
#region Binding for GridStyle
public static readonly BindableProperty GridStyleProperty = BindableProperty.Create(
propertyName: "GridStyle",
returnType: typeof(Style),
declaringType: typeof(LabelEntry),
defaultValue: null,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: GridStylePropertyChanged);
public Style GridStyle { get; set; }
private static void GridStylePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (LabelEntry)bindable;
control.cGrid.Style = (Style)newValue;
}
#endregion
}
and how I use the control
<custom:LabelEntry Style="{StaticResource SimpleLabelEntry}"
LabelText="Test Label"
EntryPlaceholder="Test Placeholder"
EntryText="{Binding TestData}"/>
where TestData is a property that implements INotifyPropertyChanged in my view model