Hello @Dani_S ,
I want to bind the scrolled event to view model, how is can be done?
You could try CommunityToolkit.Maui
NuGet package and use EventToCommandBehavior, then convert the Scrolled
event . If you want to access EventArgs
, you can check Accessing the EventArgs from the event.
Note: CommunityToolkit.Maui 3.0.0 is available for .net7. And the package should be set up first, see Getting Started with the .NET Multi-platform App UI (.NET MAUI) Community Toolkit - .NET Community Toolkit | Microsoft Learn
<ScrollView.Behaviors>
<toolkit:EventToCommandBehavior x:TypeArguments ="ScrolledEventArgs" EventName="Scrolled" Command="{Binding ScrollCommand}" ></toolkit:EventToCommandBehavior>
</ScrollView.Behaviors>
When i press on button in this view i want to set the scroll view to start, how is can be done?
You could add an action on the ViewModel, and invoke the action when click the button. Please refer to the following code:
public MainPage()
{
InitializeComponent();
MainPageViewModel VM = new MainPageViewModel();
this.BindingContext = VM;
VM.ScrollAction = () =>
{
scrollView.ScrollToAsync(TextLabel, ScrollToPosition.End,true;//TextLabel is the label in ScrollView
};
}
ViewModel
public class MainPageViewModel
{
public Action ScrollAction { get; set; }
public ICommand ButtonCommand { get; set; }// binding to your Button
public ICommand ScrollCommand { get; set; }// binding to your ScrollView
public MainPageViewModel()
{
ButtonCommand = new Command(() =>{
this.ScrollAction();// call the action
});
ScrollCommand = new Command(( e) => {
ScrolledEventArgs eventArgs = (ScrolledEventArgs)e;
Console.WriteLine("{0}----", eventArgs.ScrollY);
});
}
}
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.