Reputation: 965
I have created a custom control in xamarin and have wired up some bindable properties. However, I would like them to be aware of each other. For example, I have a type property that I want to affect a string property. Here are the 2 bindable propeties at the moment:
public string ApprovalType { get; set; }
public static readonly BindableProperty ApprovalTypeProperty = BindableProperty.Create(
propertyName: "ApprovalType",
returnType: typeof(string),
declaringType: typeof(CustomApprovalView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: ApprovalTypePropertyChanged);
private static void ApprovalTypePropertyChanged(BindableObject bindable, object oldvalue, object newValue)
{
//Do Nothing
}
public string MessageString { get; set; }
public static readonly BindableProperty MessageStringProperty = BindableProperty.Create(
propertyName: "MessageString",
returnType: typeof(string),
declaringType: typeof(CustomApprovalView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: MessageStringPropertyChanged);
private static void MessageStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
{
var control = (CustomApprovalView)bindable;
//This isn't possible as this method is static
//control.Message.Text = this.ApprovalType
}
I've tried doing it in the Get, set but when putting breakpoints there, they are not called and the label is empty:
public string RequestedDateString
{
get { return (string)GetValue(RequestedDateStringProperty); }
set
{
SetValue(RequestedDateStringProperty, value);
string type = (string)GetValue(ApprovalTypeProperty);
if (type.ToLower() == "open")
{
this.RequestedDate.Text = $"Open on ... {value}";
}
else
{
this.RequestedDate.Text = $"Closed on ... {value}";
}
}
}
public static readonly BindableProperty RequestedDateStringProperty = BindableProperty.Create(
propertyName: "RequestedDateString",
returnType: typeof(string),
declaringType: typeof(CustomApprovalView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay);
Below is the full code - Xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:pancake="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
xmlns:animations="clr-namespace:XamarinUITests.Behaviours"
x:Class="XamarinUITests.CustomApprovalView">
<pancake:PancakeView BackgroundColor="white">
<pancake:PancakeView.Resources>
<ResourceDictionary>
<Color x:Key="lightPink">#D96CB3</Color>
<Color x:Key="darkPink">#8C4F7C</Color>
<Color x:Key="offWhite">#F2F2F2</Color>
<Color x:Key="darkGrey">#454859</Color>
<Color x:Key="lightGrey">#737B8C</Color>
<Color x:Key="orange">#F2B05E</Color>
<Color x:Key="green">#42BD94</Color>
<Color x:Key="red">#F86B7B</Color>
</ResourceDictionary>
</pancake:PancakeView.Resources>
<StackLayout Orientation="Horizontal">
<BoxView x:Name="ApprovalColourBox" WidthRequest="5"/>
<StackLayout Orientation="Vertical" Padding="5" HorizontalOptions="FillAndExpand" Spacing="5">
<Label x:Name="RequestedDate" TextColor="{StaticResource lightGrey}" FontSize="Small"/>
<Label x:Name="RequestTitle" TextColor="{StaticResource darkGrey}" FontSize="Medium"/>
<Label x:Name="DatesRequested" TextColor="{StaticResource lightGrey}" FontSize="Small"/>
<pancake:PancakeView BackgroundColor="{StaticResource offWhite}" HorizontalOptions="FillAndExpand" Margin="10,5,20,10" Padding="10,5,10,5" CornerRadius="5">
<Label x:Name="Message" TextColor="{StaticResource darkGrey}" FontSize="Small" LineBreakMode="WordWrap"/>
</pancake:PancakeView>
</StackLayout>
</StackLayout>
</pancake:PancakeView>
</ContentView>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Forms.PancakeView;
namespace XamarinUITests
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomApprovalView : ContentView
{
public CustomApprovalView()
{
InitializeComponent();
}
#region ApprovalColour (Bindable Color)
public Color ApprovalColour { get; set; }
public static readonly BindableProperty ApprovalColourProperty = BindableProperty.Create(
propertyName: "ApprovalColour",
returnType: typeof(Color),
declaringType: typeof(CustomApprovalView),
defaultValue: Color.Transparent,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: ApprovalColourPropertyChanged);
private static void ApprovalColourPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
{
var control = (CustomApprovalView)bindable;
control.ApprovalColourBox.BackgroundColor = (Color)newValue;
}
#endregion ApprovalColour (Bindable Color)
#region RequestTitleString (Bindable string)
public string RequestTitleString { get; set; }
public static readonly BindableProperty RequestTitleStringProperty = BindableProperty.Create(
propertyName: "RequestTitleString ",
returnType: typeof(string),
declaringType: typeof(CustomApprovalView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: RequestTitleStringPropertyChanged);
private static void RequestTitleStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
{
var control = (CustomApprovalView)bindable;
control.RequestTitle.Text = newValue.ToString();
}
#endregion RequestTitleString (Bindable string)
#region DatesRequestedString (Bindable string)
public string DatesRequestedString { get; set; }
public static readonly BindableProperty DatesRequestedStringProperty = BindableProperty.Create(
propertyName: "DatesRequestedString",
returnType: typeof(string),
declaringType: typeof(CustomApprovalView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: DatesRequestedStringPropertyChanged);
private static void DatesRequestedStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
{
var control = (CustomApprovalView)bindable;
control.DatesRequested.Text = newValue.ToString();
}
#endregion DatesRequestedString (Bindable string)
#region MessageString (Bindable string)
public string MessageString { get; set; }
public static readonly BindableProperty MessageStringProperty = BindableProperty.Create(
propertyName: "MessageString",
returnType: typeof(string),
declaringType: typeof(CustomApprovalView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: MessageStringPropertyChanged);
private static void MessageStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
{
var control = (CustomApprovalView)bindable;
control.Message.Text = newValue.ToString();
}
#endregion MessageString (Bindable string)
#region RequestedDateString (Bindable string)
public string RequestedDateString
{
get { return (string)GetValue(RequestedDateStringProperty); }
set
{
SetValue(RequestedDateStringProperty, value);
string type = (string)GetValue(ApprovalTypeProperty);
if (type.ToLower() == "open")
{
this.RequestedDate.Text = $"Open on ... {value}";
}
else
{
this.RequestedDate.Text = $"Closed on ... {value}";
}
}
}
public static readonly BindableProperty RequestedDateStringProperty = BindableProperty.Create(
propertyName: "RequestedDateString",
returnType: typeof(string),
declaringType: typeof(CustomApprovalView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay);
//propertyChanged: RequestedDateStringPropertyChanged);
//private static void RequestedDateStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
//{
// var control = (CustomApprovalView)bindable;
// control.RequestedDate.Text = $"{newValue.ToString()}";
//}
#endregion RequestedDateString (Bindable string)
#region ApprovalType (Bindable string)
public string ApprovalType { get; set; }
public static readonly BindableProperty ApprovalTypeProperty = BindableProperty.Create(
propertyName: "ApprovalType",
returnType: typeof(string),
declaringType: typeof(CustomApprovalView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: ApprovalTypePropertyChanged);
private static void ApprovalTypePropertyChanged(BindableObject bindable, object oldvalue, object newValue)
{
//Do Nothing
}
#endregion ApprovalType (Bindable string)
}
}
ContentPage.xaml:
<Label Text="Pending Approvals" HorizontalOptions="Start" TextColor="{StaticResource darkGrey}" Margin="10,10,0,5"/>
<ListView x:Name="pendingApprovalList" ItemsSource="{Binding PastApprovals}" HasUnevenRows="True" SeparatorVisibility="None" BackgroundColor="{StaticResource offWhite}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<local:CustomApprovalView Margin="0,0,0,10"
ApprovalType="Open"
MessageString="{Binding Message}"
ApprovalColour="{Binding Colour}"
RequestedDateString="{Binding RequestedDate}"
RequestTitleString="{Binding RequestTitle}"
DatesRequestedString="{Binding DatesRequested}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
Upvotes: 0
Views: 456
Reputation: 18861
You could handle the logic in the set method of MessageString
public string MessageString
{
get { return (string)GetValue (MessageStringProperty ); }
set {
// do something you want
SetValue (MessageStringProperty, value);
}
}
Upvotes: 1