gpt4 book ai didi

c# - 如何在代码后面获取 ContentPresenter?

转载 作者:行者123 更新时间:2023-11-30 22:31:19 26 4
gpt4 key购买 nike

我在 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>

最佳答案

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

private ContentPresenter cPresenter;

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

关于c# - 如何在代码后面获取 ContentPresenter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9294044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com