Best Practice for Handling Parent Control Behavior in Custom User Control

Muthu S 20 Reputation points
2024-02-13T06:07:59.97+00:00

"I'm developing a custom user control and need to manage the behavior of its parent UI element based on the triggers inside this control. Currently, I use an event handler property within the custom control. Alternatively, I've tried passing the parent UI element as a dependency property to the custom control itself. Both methods work, but I'm unsure about the best practice. Any advice on which approach is recommended?" Approach 1:
public class CustomControl : UserControl
{
public event PointerEventHandler PointerWheelChanged;

// Method to trigger the event
protected void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
PointerWheelChanged?.Invoke(this, e);
}
}

ParentPage.xaml:

<ScrollViewer x:Name="DetailsScrollViewer">
<CustomControl PointerWheelChanged="OnPointerWheelChangedEvent"/>
</ScrollViewer>

ParentPage.cs file:

protected void OnPointerWheelChangedEvent(object sender, PointerRoutedEventArgs e)
{
Microsoft.UI.Input.PointerPoint point = e.GetCurrentPoint(DetailsScrollViewer);
DetailsScrollViewer.ScrollToVerticalOffset(DetailsScrollViewer.VerticalOffset - point.Properties.MouseWheelDelta);
}

Approach 2:
public class CustomControl : UserControl
{
// Dependency property to hold reference to ScrollViewer
public ScrollViewer ScrollViewer
{
get { return (ScrollViewer)GetValue(ScrollViewerProperty); }
set { SetValue(ScrollViewerProperty, value); }
}

public static readonly DependencyProperty ScrollViewerProperty =
DependencyProperty.Register("ScrollViewer", typeof(ScrollViewer), typeof(CustomControl), new PropertyMetadata(null));

// Method to handle the event
protected void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
Microsoft.UI.Input.PointerPoint point = e.GetCurrentPoint(ScrollViewer);
ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset - point.Properties.MouseWheelDelta);
}
}

ParentPage.xaml:

<ScrollViewer x:Name="DetailsScrollViewer">
<CustomControl ScrollViewer="DetailsScrollViewer"/>
</ScrollViewer>

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
696 questions
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 13,276 Reputation points Microsoft Vendor
    2024-02-13T09:23:25.4166667+00:00

    Hi @Muthu S

    Welcome to Microsoft Q&A!

    Both methods are correct, it mainly depends on your development needs.

    I think approach 2 is more elegant. The approach2 is more like a templated control, the code is easier to understand, and it facilitates subsequent development and maintenance.

    Thank you.


    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