As I realized, you have a custom WPF control that has a ScrollViewer
in its ControlTemplate and, you want to add an event-handler to the ScrollChanged
event of the ScrollViewer
.
Since the RoutedEvent is bubbled from the control to the window via tha control's parents, you can catch the event in each parent of the path. So, if you want to handle this event in the window that holds your ComboBoxDatagrid
control, you can catch the ScrollViewer.ScrollChanged
event, in the ComboBoxDatagrid
control like the following:
<myControls:ComboBoxDatagrid x:Name="cbdg1"
ScrollViewer.ScrollChanged="cbdg1_ScrollChanged" />
and, in the event-handler you can use the vertical-offset like the following:
private void cbdg1_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
double d = e.VerticalOffset;
}
If you want to add an event-handler to the ScrollViewer.ScrollChanged
event, in the window's code-behind, you can use the AddHandler method like the following:
public MainWindow()
{
InitializeComponent();
cbdg1.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(cbdg1_ScrollChanged));
}
If you want to add an event-handler in the control's code, you can set a name to the ScrollViewer
in the control's template like the following:
<Style TargetType="{x:Type myControls:ComboBoxDatagrid}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type myControls:ComboBoxDatagrid}">
<Grid>
<ScrollViewer x:Name="PART_ScrollViewer">
</ScrollViewer>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and use the GetTemplateChild method in order to get the ScrollViewer
from the control's template like the following:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
ScrollViewer scrollViewer = GetTemplateChild("PART_ScrollViewer") as ScrollViewer;
scrollViewer.ScrollChanged += OnScrollChanged;
}
void OnScrollChanged(object sender, ScrollChangedEventArgs e)
{
double d = e.VerticalOffset;
}