Accessing an element defined in a style's template in wpf?

两盒软妹~` 提交于 2019-12-25 16:12:51

问题


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.

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 />
                        .......

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


回答1:


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

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


来源:https://stackoverflow.com/questions/1389594/accessing-an-element-defined-in-a-styles-template-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!