访问 wpf 中样式模板中定义的元素? [英] Accessing an element defined in a style's template in wpf?

查看:32
本文介绍了访问 wpf 中样式模板中定义的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wpf TabControl,我在其中添加了一个 ScrollViewer.这一切都封装在一个位于资源字典中的样式中.

I have a wpf TabControl which I added a ScrollViewer to. This is all wrapped into a style which is situated in a resource dictionary.

现在,在窗口的 xaml 端,我要做的就是设置.

Now, on the window's xaml side, all I do is set .

我想访问控件查看器元素,在样式中定义如下:

I would like to access the control viewer element, as defined in the style as follows:

<Style x:Key="MyTabStyle" TargetType="{x:Type TabControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                 <ScrollViewer />
                        .......

如何访问在层次结构中嵌套如此深的 ScrollViewer?

How do I access the ScrollViewer which is so deeply nested in the hierarchy?

推荐答案

给它一个名字(例如 theScrollViewer),然后使用 FindName 方法访问它:

Give it a name (theScrollViewer for instance), and use the FindName method to access it :

ScrollViewer scrollViewer = theTabControl.Template.FindName("theScrollViewer") as ScrollViewer;

这篇关于访问 wpf 中样式模板中定义的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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