Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BindingContext to Behaviors using Bindings (.NET 9) #486

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/maui/behaviors/animation-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ The following example shows how to attach the `AnimationBehavior` to an `Image`
#### View

```xaml
<Image Source="thumbs-up.png">
<Image Source="thumbs-up.png" x:Name="ThumbsUpImage">
<Image.Behaviors>
<toolkit:AnimationBehavior Command="{Binding ThumbsUpCommand}">
<toolkit:AnimationBehavior
Command="{Binding ThumbsUpCommand}">
BindingContext="{Binding Path=BindingContext, Source={x:Reference ThumbsUpImage}, x:DataType=Image}"
<toolkit:AnimationBehavior.AnimationType>
<toolkit:FadeAnimation />
</toolkit:AnimationBehavior.AnimationType>
Expand Down Expand Up @@ -151,9 +153,12 @@ The following example shows how to add the `AnimationBehavior` to an `Entry`, bi

```xaml
<Entry Placeholder="First name (Required)"
Text="{Binding FirstName}">
Text="{Binding FirstName}"
x:Name="FirstNameEntry">
<Entry.Behaviors>
<toolkit:AnimationBehavior AnimateCommand="{Binding TriggerAnimationCommand}">
<toolkit:AnimationBehavior
AnimateCommand="{Binding TriggerAnimationCommand}">
BindingContext="{Binding Path=BindingContext, Source={X:Reference FirstNameEntry}, x:DataType=Entry}"
<toolkit:AnimationBehavior.AnimationType>
<toolkit:FadeAnimation />
</toolkit:AnimationBehavior.AnimationType>
Expand Down
10 changes: 7 additions & 3 deletions docs/maui/behaviors/event-to-command-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ The `EventToCommandBehavior` can be used as follows in XAML:
x:Class="MyLittleApp.MainPage"
x:Name="Page">

<Button>
<Button x:Name="MyButton">
<Button.Behaviors>
<toolkit:EventToCommandBehavior
EventName="Clicked"
Command="{Binding Source={x:Reference Page}, Path=BindingContext.MyCustomCommand}" />
BindingContext="{Binding Path=BindingContext, Source={x:Reference MyButton}, x:DataType=Button}
Command="{Binding Source={x:Reference Page}, Path=BindingContext.MyCustomCommand, x:DataType=ContentPage}" />
</Button.Behaviors>
</Button>
</ContentPage>
Expand Down Expand Up @@ -99,11 +100,14 @@ By using the `EventToCommandBehavior<T>` implementation the `EventArgs` will be
The following example shows how to use the generic implementation to pass the `WebNavigatedEventArgs` into the command.

```xaml
<WebView Source="https://github.com">
<WebView
Source="https://github.com">
x:Name="MyWebView"
<WebView.Behaviors>
<toolkit:EventToCommandBehavior
x:TypeArguments="WebNavigatedEventArgs"
EventName="Navigated"
BindingContext="{Binding Path=BindingContext, Source={x:Reference MyWebView}, x:DataType=WebView}"
Command="{Binding WebViewNavigatedCommand}" />
</WebView.Behaviors>
</WebView>
Expand Down
2 changes: 1 addition & 1 deletion docs/maui/behaviors/image-touch-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The following examples show how to add the `ImageTouchBehavior` to an `Image` an
<Image HeightRequest="100" WidthRequest="100">
<Image.Behaviors>
<toolkit:ImageTouchBehavior
Command="{Binding Source={x:Reference Page}, Path=BindingContext.IncreaseTouchCountCommand}"
Command="{Binding Source={x:Reference Page}, Path=BindingContext.IncreaseTouchCountCommand, x:DataType=Image}"
DefaultImageSource="button.png"
HoveredImageSource="button_hovered.png"
PressedImageSource="button_pressed.png" />
Expand Down
8 changes: 5 additions & 3 deletions docs/maui/behaviors/maximum-length-reached-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ The `MaxLengthReachedBehavior` can be used as follows in XAML:
x:Name="Page">

<Entry Placeholder="Start typing until MaxLength is reached..."
MaxLength="100">
MaxLength="100"
x:Name="MaxLengthEntry">
<Entry.Behaviors>
<toolkit:MaxLengthReachedBehavior
Command="{Binding Source={x:Reference Page}, Path=BindingContext.MaxLengthReachedCommand}" />
<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>

Expand Down
2 changes: 1 addition & 1 deletion docs/maui/behaviors/progressbar-animation-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The `ProgressBarAnimationBehavior` can be used as follows in XAML:
<ProgressBar.Behaviors>
<toolkit:ProgressBarAnimationBehavior
x:Name="ProgressBarAnimationBehavior"
Progress="{Binding Source={x:Reference Page}, Path=BindingContext.Progress}"
Progress="{Binding Source={x:Reference Page}, Path=BindingContext.Progress, x:DataType=ProgressBar}"
Length="250"/>
</ProgressBar.Behaviors>
</ProgressBar>
Expand Down
15 changes: 11 additions & 4 deletions docs/maui/behaviors/touch-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ The following example shows how to add the `TouchBehavior` to a `HorizontalStack
<HorizontalStackLayout
Padding="20"
Background="Black"
HorizontalOptions="Center">
HorizontalOptions="Center"
x:Name="TouchableHorizontalStackLayout">
<HorizontalStackLayout.Behaviors>
<toolkit:TouchBehavior
BindingContext="{Binding Source={x:Reference Page}, Path=BindingContext, x:DataType=ContentPage}
LongPressDuration="750"
LongPressCommand="{Binding Source={x:Reference Page}, Path=BindingContext.IncreaseLongPressCountCommand}"/>
LongPressCommand="{Binding Source={x:Reference Page}, Path=BindingContext.IncreaseLongPressCountCommand, x:DataType=ContentPage}"/>
</HorizontalStackLayout.Behaviors>
</HorizontalStackLayout>
```
Expand Down Expand Up @@ -353,14 +355,18 @@ The equivalent `TouchBehavior` in .NET MAUI would look like this:
#### [XAML](#tab/touchbehavior-xaml)

```xaml
<HorizontalStackLayout HorizontalOptions="Center" VerticalOptions="Center">
<HorizontalStackLayout
HorizontalOptions="Center"
VerticalOptions="Center"
x:Name="TouchableHorizontalLayout">
<HorizontalStackLayout.Behaviors>
<toolkit:TouchBehavior
DefaultAnimationDuration="250"
DefaultAnimationEasing="{x:Static Easing.CubicInOut}"
PressedOpacity="0.6"
PressedScale="0.8"
Command="{Binding Command}" />
BindingContext="{Binding Path=BindingContext, Source={x:Reference TouchableHorizontalLayout}, x:DataType=HorizontalStackLayout}"
Command="{Binding LayoutTappedCommand}" />
</HorizontalStackLayout.Behaviors>

<ContentView
Expand Down Expand Up @@ -415,6 +421,7 @@ var stackLayout = new HorizontalStackLayout()
PressedOpacity = 0.6,
PressedScale = 0.8,
Command = command,
BindingContext = this.BindingContext
}
}
};
Expand Down
7 changes: 5 additions & 2 deletions docs/maui/behaviors/user-stopped-typing-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ The `UserStoppedTypingBehavior` can be used as follows in XAML:
x:Class="MyLittleApp.MainPage"
x:Name="Page">

<Entry Placeholder="Start typing when you stop the behavior will trigger...">
<Entry
Placeholder="Start typing when you stop the behavior will trigger..."
x:Name="UserStoppedTypingEntry">
<Entry.Behaviors>
<toolkit:UserStoppedTypingBehavior
Command="{Binding Source={x:Reference Page}, Path=BindingContext.SearchCommand}"
BindingContext="{Binding Path=BindingContext, Source={x:Reference UserStoppedTypingEntry}, x:DataType=Entry}"
Command="{Binding SearchCommand}"
StoppedTypingTimeThreshold="1000"
MinimumLengthThreshold="3"
ShouldDismissKeyboardAutomatically="True" />
Expand Down