【问题标题】:WPF Control Template bind to Child ControlWPF 控件模板绑定到子控件
【发布时间】:2012-08-18 06:16:27
【问题描述】:

在 TreeViewItem 控件模板中,我需要将视觉状态绑定到子控件,但我无法弄清楚绑定语法。

在 TreeView 的 HierarchicalDataTemplate 我有:

       <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <StackPanel>
                    <Image Name="imgPicture" Source=".."/>                      
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>

我想给控件模板中的imgPicture绑定一个视觉状态动画。

在 TreeViewItem 的控件模板中,我有:

<VisualState Name="Selected">
     <Storyboard>
           <DoubleAnimation Storyboard.TargetName="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header}" 
Storyboard.TargetProperty="MaxHeight"
To="100"
Duration="0"/>
     </Storyboard>
</VisualState>

但是 Storyboard.TargetName 绑定到 StackPanel(因为那是 TreeViewItem 的标题),我需要它绑定到 StackPanel 内的控件(imgPicture),以便我可以更改控件的属性。有没有办法通过使用 xaml 绑定语法来做到这一点?非常感谢您的帮助。

【问题讨论】:

  • Doesnt Storyboard.TargetName="imgPicture"` 工作吗?
  • 我尝试了 Storyboard.TargetName="imgPicture",但它返回异常:在 'System.Windows.Controls.StackPanel' 的名称范围内找不到 'imgPicture' 名称

标签: wpf templates binding controls


【解决方案1】:

您可以使用 ElementName 属性 (imgPicture) 或将 Storyboard.TargetName 设置为 imgPicture 来创建绑定。

【讨论】:

  • 我尝试了 Storyboard.TargetName="imgPicture",但它返回异常:在“System.Windows.Controls.StackPanel”的名称范围内找不到“imgPicture”名称。也尝试设置 ElementName 但它不起作用。
猜你喜欢
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 2015-09-07
  • 2011-10-29
相关资源
最近更新 更多