Hover image button in data template of collection view provide cursor hand

Dani_S 4,501 Reputation points
2023-10-05T10:39:39.13+00:00

Hi,

Why the bold is :

Microsoft.UI.Xaml.Controls.Button btn = (Microsoft.UI.Xaml.Controls.Button)handler.PlatformView;

and not(not complied)

Microsoft.UI.Xaml.Controls.ImageButton = (Microsoft.UI.Xaml.Controls.ImageButton)handler.Platform
Microsoft.Maui.Handlers.ImageButtonHandler.Mapper.AppendToMapping("Custom", (handler, view) =>
            {
#if WINDOWS
            ImageButton button = handler.VirtualView as ImageButton;
            button.HandlerChanged    += (sender, args) =>
            {
                Microsoft.UI.Xaml.Controls.Button btn = (Microsoft.UI.Xaml.Controls.Button)handler.PlatformView;
                btn.Loaded += (s2, e2) =>
                {
                    MainThread.BeginInvokeOnMainThread(() =>
                    {
                        btn.ChangeCursor(Microsoft.UI.Input.InputSystemCursor.Create(Microsoft.UI.Input.InputSystemCursorShape.Hand));
                    });
                };

            };
#endif


public static class ElementExtension
    {
#if WINDOWS
        public static void ChangeCursor(this UIElement uiElement, InputCursor cursor)
        {
            Type type = typeof(UIElement);
            type.InvokeMember("ProtectedCursor", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, uiElement, new object[] { cursor });
        }
 #endif
Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-10-06T03:15:35.4866667+00:00

    Hello,

    This is because the ImageButton itself is a special Button implemented by encapsulating the Button.

    Therefore, the ImageButton class does not actually exist in the Microsoft.UI.Xaml.Controls Namespace.

    Since the ImageButton is obtained by encapsulating the Button, it is necessary to modify the implementation through the Button when modifying the ImageButton.

    Best Regards,

    Alec Liu.


    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.