Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi To All

I making a ComboBoxDatagrid(A datagrid inside combobox) with CustomControlLibrary using WPF.

But When I Add a Datagrid in ResourceDictionary (Generic.Xaml), cant Access To ScrollViewer.ScrollChanged Event to Find VerticalOffset Value Of Datagrid Scroll

I need to this Value for Take And...

But Does Not Access To This Event! Please Help Me on this?

Thanks To All
Posted
Updated 27-Oct-11 0:36am
v2
Comments
DiacoDecom 27-Oct-11 2:20am    
No Any Answer To Help me ?!!!؟

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:


XML
<myControls:ComboBoxDatagrid x:Name="cbdg1"
                                    ScrollViewer.ScrollChanged="cbdg1_ScrollChanged" />

and, in the event-handler you can use the vertical-offset like the following:


C#
private void cbdg1_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    double d = e.VerticalOffset;

    // Here you can use the vertical-offset's value.
}

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:


C#
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:


XML
<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:


C#
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;

    // Here you can use the vertical-offset's value.
}

 
Share this answer
 
v2
hi
Tanks

But I want Use ScrollChanged Event OF Datagrid
That Only Can Access in Xaml Not In CodeBehind
Such as :
Datagrid1.ScrollViewer.ScrollChanged += new ......

only in xaml can Access the ScrollViewer.ScrollChange Event

Is there Way to Initialize From Code Behind Such as Bellow :
Datagrid1.ScrollViewer.ScrollChanged += new ......

>>============================<<

Thanks
 
Share this answer
 
Comments
Shmuel Zang 27-Oct-11 11:55am    
Please don't post questions as answers. I have an answer to this question. But, remove this solution and, post your question as a comment to my solution. I'll answer to you there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900