如何在代码中获取ContentPresenter?

时间:2012-02-15 13:08:54

标签: c# wpf silverlight xaml

我在 Themes \ Generic.xaml

下面有以下代码

现在我需要在代码中获取 cPresenter ,我将如何做到这一点?

实际上我尝试将Silverlight实现转换为WPF代码。 我想使用像Silverlight这样的东西:

FrameworkElement cp = this.GetTemplateChild("cPresenter") as FrameworkElement;

主题\ Generic.xaml

<Style TargetType="local:Marquee">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:Marquee">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">    
                        <ScrollViewer Width="Auto" Height="Auto" HorizontalContentAlignment="Stretch" 
                             VerticalContentAlignment="Stretch" VerticalScrollBarVisibility="Disabled">
                            <Canvas>
                                <ContentPresenter  x:Name="cPresenter" VerticalAlignment="Center" HorizontalAlignment="Center"/>                            
                            </Canvas>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:3)

没有理由不应该在WPF中工作。 将其放在Marquee的代码中。

private ContentPresenter cPresenter;

public override void OnApplyTemplate()
{
   base.OnApplyTemplate();
   this.cPresenter = this.GetTemplateChild("cPresenter") as ContentPresenter;
}