Skip to content

Instantly share code, notes, and snippets.

@mr5z
Last active March 28, 2022 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr5z/5ee1c5915c55ae325dfd3ef0c0b0d1d0 to your computer and use it in GitHub Desktop.
Save mr5z/5ee1c5915c55ae325dfd3ef0c0b0d1d0 to your computer and use it in GitHub Desktop.
public static class BindableHelper
{
private const string KnownPropertyPattern = "Property";
public static BindableProperty CreateProperty<T>(
T? defaultValue = default,
BindingMode mode = BindingMode.TwoWay,
BindableProperty.BindingPropertyChangedDelegate? propertyChanged = null,
[CallerMemberName] string? propertyName = null)
{
propertyName = RemoveLastOccurrence(propertyName!, KnownPropertyPattern);
return BindableProperty.Create(
propertyName,
typeof(T),
typeof(BindableObject),
defaultValue,
mode,
propertyChanged: propertyChanged);
}
private static string RemoveLastOccurrence(string source, string toFind)
{
var index = source.LastIndexOf(toFind);
return index == -1 ? source : source.Substring(0, index);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment