Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The MaxLengthReachedBehavior
is a Behavior
that allows the user to trigger an action when a user has reached the maximum length allowed on an InputView
. It can either trigger a Command
or an event depending on the user's preferred scenario. Both the Command
and event will include the resulting text of the InputView
.
Additionally it is possible to dismiss the keyboard when the maximum length is reached via the ShouldDismissKeyboardAutomatically
property which defaults to false
.
Important
The .NET MAUI Community Toolkit Behaviors do not set the BindingContext
of a behavior, because behaviors can be shared and applied to multiple controls through styles. For more information refer to .NET MAUI Behaviors
In order to use the toolkit in XAML the following xmlns
needs to be added into your page or view:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Therefore the following:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
</ContentPage>
Would be modified to include the xmlns
as follows:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
</ContentPage>
The MaxLengthReachedBehavior
can be used as follows in XAML:
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="CommunityToolkit.Maui.Sample.Pages.Behaviors.MaxLengthReachedBehaviorPage"
x:Name="Page">
<Entry Placeholder="Start typing until MaxLength is reached..."
MaxLength="100"
x:Name="MaxLengthEntry">
<Entry.Behaviors>
<toolkit:MaxLengthReachedBehavior
BindingContext="{Binding Path=BindingContext, Source={x:Reference MaxLengthEntry}, x:DataType=Entry}"
Command="{Binding Source={x:Reference Page}, Path=BindingContext.MaxLengthReachedCommand, x:DataType=ContentPage}" />
</Entry.Behaviors>
</Entry>
</ContentPage>
The MaxLengthReachedBehavior
can be used as follows in C#:
class MaxLengthReachedBehaviorPage : ContentPage
{
public MaxLengthReachedBehaviorPage()
{
var entry = new Entry
{
Placeholder = "Start typing until MaxLength is reached...",
MaxLength = 100
};
var behavior = new MaxLengthReachedBehavior();
behavior.SetBinding(
MaxLengthReachedBehavior.CommandProperty,
static (MaxLengthReachedBehaviorViewModel vm) => vm.MaxLengthReachedCommand,
source: this.BindingContext);
entry.Behaviors.Add(behavior);
Content = entry;
}
}
Our CommunityToolkit.Maui.Markup
package provides a much more concise way to use this Behavior
in C#.
using CommunityToolkit.Maui.Markup;
class MaxLengthReachedBehaviorPage : ContentPage
{
public MaxLengthReachedBehaviorPage()
{
Content = new Entry
{
Placeholder = "Start typing until MaxLength is reached...",
MaxLength = 100
}.Behaviors(
new MaxLengthReachedBehavior()
.Bind(MaxLengthReachedBehavior.CommandProperty,
getter: static (ViewModel vm) => vm.MaxLengthReachedCommand,
source: this.BindingContext));
}
}
Property | Type | Description |
---|---|---|
Command |
ICommand | The command that is executed when the user has reached the maximum length. The parameter of the command will contain the Text of the InputView . |
ShouldDismissKeyboardAutomatically |
bool |
Indicates whether or not the keyboard should be dismissed automatically when the maximum length is reached. |
Event | Description |
---|---|
MaxLengthReached |
The event that is raised when the user has reached the maximum length. The event args will contain the Text of the InputView . |
You can find an example of this behavior in action in the .NET MAUI Community Toolkit Sample Application.
You can find the source code for MaxLengthReachedBehavior
over on the .NET MAUI Community Toolkit GitHub repository.
.NET MAUI Community Toolkit feedback
.NET MAUI Community Toolkit is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Create a UI in a .NET MAUI app by using XAML - Training
Learn how to design a UI for a .NET MAUI app using XAML.
Documentation
NumericValidationBehavior - .NET MAUI Community Toolkit - Community Toolkits for .NET
The NumericValidationBehavior is a Behavior that allows the user to determine if text input is a valid numeric value.
CharactersValidationBehavior - .NET MAUI Community Toolkit - Community Toolkits for .NET
The CharactersValidationBehavior is a Behavior that allows the user to validate text input depending on specified parameters.
RequiredStringValidationBehavior - .NET MAUI Community Toolkit - Community Toolkits for .NET
The RequiredStringValidationBehavior is a Behavior that allows the user to determine if text input is equal to specific text.