【问题标题】:Accessing an element defined in a style's template in wpf?访问在 wpf 的样式模板中定义的元素?
【发布时间】:2009-09-07 14:08:04
【问题描述】:

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

现在,在窗口的 xaml 端,我所做的只是设置 .

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

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

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

【问题讨论】:

    标签: wpf templates styles


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-22
      • 2023-03-27
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多