如何从外部模板控制模板内的元素

时间:2010-02-03 12:13:16

标签: silverlight templates vsm

我很难找到解决这个问题的方法。我有一个控件模板,其中我有一个内容展示器和一个自定义可视状态管理器,在SelectionStates组下具有可视状态Selected和UnSelected。在内容演示者的内容模板中,我有一个椭圆,其Fill属性我想根据视觉状态进行动画处理。这不是直接可能的,因为椭圆位于内容呈现器的内容模板内。是否有任何间接的解决方法可以做同样的事情。以下是我的模板

<Style TargetType="local:TabNavigationItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TabNavigationItem">
                <Grid>
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="SelectionStates">
                            <vsm:VisualState x:Name="Selected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TabStripEllipse"
                                                   Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="#FF3B5A82"/>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="UnSelected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TabStripEllipse"
                                                   Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Transparent"/>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>

                    <ContentPresenter>
   <ContentPresenter.ContentTemplate>
    <DataTemplate x:Key="tabNavigationItemTemplate">
     <Border Padding="1">
      <Ellipse x:Name="TabStripEllipse" 
       Fill="Transparent"
       Stroke="#FF3B5A82" Cursor="Hand" 
       Height="8" Width="8"/>
     </Border>
    </DataTemplate>
   </ContentPresenter.ContentTemplate>
  </ContentPresenter>

 </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

您的意见和建议值得赞赏..

您可能还想将我的xaml文件设置如下..但是内部数据模板应该可以访问与外部模板的目标类型相关的属性。

<Style TargetType="local:TabNavigationItem">
    <Setter Property="ItemContentTemplate" Value="{StaticResource contentTemplate}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TabNavigationItem">
                <Grid>

                    <Border>
                        <ContentPresenter>
                            <ContentPresenter.ContentTemplate>

                                <vsm:VisualStateManager.VisualStateGroups>
                                    <vsm:VisualStateGroup x:Name="SelectionStates">
                                        <vsm:VisualState x:Name="Selected">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TabItemPresenter"
                                                               Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <SolidColorBrush Color="#FF3B5A82"/>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="UnSelected">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TabItemPresenter"
                                                               Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <SolidColorBrush Color="Transparent"/>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>
                                </vsm:VisualStateManager.VisualStateGroups>

                                <Border Padding="1">
                                    <Ellipse x:Name="TabStripEllipse" 
                                        Fill="Transparent"
                                        Stroke="#FF3B5A82" Cursor="Hand" 
                                        Height="8" Width="8"/>
                                </Border>

                            </ContentPresenter.ContentTemplate>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

也许您有理由以这种方式使用ContentPresenter。内容呈现器的正常使用将充当内容被绑定到控件的属性的占位符。您通常不会使用ContentPresenter,然后为其提供自己的DataTemplate。这是我的Xaml版本,没有明显不必要的内容演示者: -

<Style TargetType="local:TabNavigationItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TabNavigationItem">
                <Grid>
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="SelectionStates">
                            <vsm:VisualState x:Name="Selected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TabStripEllipse"
                                                   Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="#FF3B5A82"/>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="UnSelected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TabStripEllipse"
                                                   Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Transparent"/>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>    
     <Border Padding="1">
      <Ellipse x:Name="TabStripEllipse" 
       Fill="Transparent"
       Stroke="#FF3B5A82" Cursor="Hand" 
       Height="8" Width="8"/>
     </Border>    
 </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在你的VSM应该能够找到Ellipse。