问题
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