如何在代码后面获取ContentPresenter?

发布于 2025-01-06 00:40:13 字数 1443 浏览 0 评论 0原文

我在 Themes\Generic.xaml 下有以下代码,

现在我需要在代码后面获取 cPresenter 我该怎么做?

事实上,我尝试将 Silverlight 实现转换为 WPF 代码。 我想使用 Silverlight 之类的东西:

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

Themes\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>

I have the code below under Themes\Generic.xaml

Now I need to get cPresenter in code behind how I can do it?

In fact I try to convert Silverlight implementation into WPF code.
And I want to use something like Silverlight has:

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

Themes\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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2025-01-13 00:40:13

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

private ContentPresenter cPresenter;

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

There's no reason that shouldn't work in WPF also.
Put this in your code for Marquee.

private ContentPresenter cPresenter;

public override void OnApplyTemplate()
{
   base.OnApplyTemplate();
   this.cPresenter = this.GetTemplateChild("cPresenter") as ContentPresenter;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文