使用WPF的CustomControl中的Datagrid ScrollViewer.ScrollChanged事件 [英] Datagrid ScrollViewer.ScrollChanged Event in CustomControl using WPF

查看:505
本文介绍了使用WPF的CustomControl中的Datagrid ScrollViewer.ScrollChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,全部

我使用WPF使用CustomControlLibrary制作了ComboBoxDatagrid(A datagrid inside combobox).

但是,当我在ResourceDictionary (Generic.Xaml)中添加Datagrid时,无法访问ScrollViewer.ScrollChanged事件以查找VerticalOffset Datagrid Scroll的值

我需要这个取值......

但是无法访问此事件!请帮我吗?

感谢全部

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

推荐答案

我意识到,您有一个自定义WPF控件,其中有ScrollViewer href ="http://msdn.microsoft.com/en-us/library/aa970773.aspx"> ControlTemplate ,并且您想向ScrollViewerScrollChanged事件添加事件处理程序

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.

由于 RoutedEvent 通过tha控件从控件冒泡到窗口父级,您可以在路径的每个父级中捕获事件.因此,如果要在保存ComboBoxDatagrid控件的窗口中处理此事件,可以在ComboBoxDatagrid控件中捕获ScrollViewer.ScrollChanged事件,如下所示:

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;

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

如果要将事件处理程序添加到ScrollViewer.ScrollChanged事件,请在窗口的代码后面,使用

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)); 
}

如果要在控件的代码中添加事件处理程序,可以在控件的模板中为ScrollViewer设置名称,如下所示:

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>

并使用 GetTemplateChild 方法,以便从控件模板中获取ScrollViewer,如下所示:

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;

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



坦克

但是我想使用DataGrid的ScrollChanged事件
只能在Xaml中访问而在CodeBehind中不能访问
如:
Datagrid1.ScrollViewer.ScrollChanged + =新......

仅在xaml中可以访问ScrollViewer.ScrollChange事件

有没有办法从下面的代码中初始化诸如Bellow的代码:
Datagrid1.ScrollViewer.ScrollChanged + =新......

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

谢谢
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


这篇关于使用WPF的CustomControl中的Datagrid ScrollViewer.ScrollChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆