Skip to content

Commit

Permalink
Merge pull request #4 from starkos/fix/autofocus-focuswhenprop
Browse files Browse the repository at this point in the history
Rename AutoFocusBehavior.When to FocusWhen
  • Loading branch information
starkos committed Oct 29, 2019
2 parents fb14c79 + cc72060 commit 53464e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
20 changes: 11 additions & 9 deletions Industrious.Forms/AutoFocusBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Industrious.Forms
/// <code>
/// &lt;Entry
/// Text="{Binding Title}"
/// local:AutoFocusBehavior.When="{Binding ShouldFocusTitle}" /&gt;
/// local:AutoFocusBehavior.FocusWhen="{Binding ShouldFocusTitle}" /&gt;
/// </code>
/// </example>
/// <example>
Expand All @@ -22,35 +22,37 @@ namespace Industrious.Forms
/// <code>
/// &lt;Entry
/// Text="{Binding Title}"
/// local:AutoFocusBehavior.When="true" /&gt;
/// local:AutoFocusBehavior.FocusWhen="True" /&gt;
/// </code>
/// </example>
public class AutoFocusBehavior : Behavior<InputView>
{
public static readonly BindableProperty WhenProperty = BindableProperty.CreateAttached(
"When",
typeof(bool),
public static readonly BindableProperty FocusWhenProperty = BindableProperty.CreateAttached(
"FocusWhen",
typeof(Boolean),
typeof(AutoFocusBehavior),
false,
propertyChanged: OnWhenChanged);
propertyChanged: OnFocusWhenChanged);


public static Boolean GetFocusWhen(BindableObject bindable)
{
var value = (Boolean)bindable.GetValue(WhenProperty);
var value = (Boolean)bindable.GetValue(FocusWhenProperty);
return (value);
}

public static void SetFocusWhen(BindableObject bindable, Boolean value)
{
bindable.SetValue(WhenProperty, value);
bindable.SetValue(FocusWhenProperty, value);
}


private static void OnWhenChanged(BindableObject bindable, Object oldValue, Object newValue)
private static void OnFocusWhenChanged(BindableObject bindable, Object oldValue, Object newValue)
{
if ((Boolean)newValue)
{
_ = SetFocus((InputView)bindable);
}
}


Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ Automatically set the input focus to a particular control when a condition becom
<StackLayout>
<Entry
Text="{Binding Title}"
forms:AutoFocusBehavior.When="{Binding ShouldFocusTitle"} />
forms:AutoFocusBehavior.FocusWhen="{Binding ShouldFocusTitle}" />
</StackLayout>
</ContentView>
```

A constant can also be used to always set the input focus when the form is loaded.

```xml
<Entry
Text="{Binding Title}"
forms:AutoFocusBehavior.FocusWhen="True" />
```

### BooleanBindingExtension

A shortcut for binding boolean properties on a Xamarin.Forms view to a non-boolean value on the view model. See [BooleanConverter](#booleanconverter) for info on how values are interpreted. Supports the use of "!" to negate the value.
Expand Down

0 comments on commit 53464e1

Please sign in to comment.