对 ControlTemplate 内的控件的引用

发布于 2024-09-06 20:57:26 字数 625 浏览 1 评论 0原文

如何在代码隐藏中形成构造函数来获取对下面 XAML 中 OuterBorder 控件的引用?

<Window Template="{DynamicResource WindowTemplate}">
    <Window.Resources>      
        <ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}">
            <AdornerDecorator>
                <Border Name="OuterBorder" Background="Black" BorderBrush="Red" BorderThickness="1" CornerRadius="0">
                    <!-- Implementation here... -->
                </Border>
            </AdornerDecorator>
        </ControlTemplate>
    </Window.Resources>
</Window>

How do I, form my contructor in the code-behind get a reference to the OuterBorder control in the XAML below?

<Window Template="{DynamicResource WindowTemplate}">
    <Window.Resources>      
        <ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}">
            <AdornerDecorator>
                <Border Name="OuterBorder" Background="Black" BorderBrush="Red" BorderThickness="1" CornerRadius="0">
                    <!-- Implementation here... -->
                </Border>
            </AdornerDecorator>
        </ControlTemplate>
    </Window.Resources>
</Window>

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

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

发布评论

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

评论(2

真心难拥有 2024-09-13 20:57:26

两种可能的解决方案:

解决方案 1

将 Loaded 事件放入 XAML 中

<Border Name="OuterBorder" Loaded="Border_Loaded" ...

,并在后面的代码中将其存储在私有字段中:

private Border border;

void Border_Loaded(object sender, RoutedEventArgs e)
{
    this.border = (Border)sender;
}

或者:

解决方案 2

覆盖窗口的 OnApplyTemplate:

private Border border;

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    this.border = (Border) Template.FindName("OuterBorder", this);
}

Two possible solutions:

Solution 1

Put a Loaded event in XAML

<Border Name="OuterBorder" Loaded="Border_Loaded" ...

And in code behind store it in a private field:

private Border border;

void Border_Loaded(object sender, RoutedEventArgs e)
{
    this.border = (Border)sender;
}

OR:

Solution 2

Override the OnApplyTemplate of your Window:

private Border border;

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    this.border = (Border) Template.FindName("OuterBorder", this);
}
紅太極 2024-09-13 20:57:26

您可能需要重新考虑您的方法。你想做什么?

一般来说,您不应该希望或不需要从代码隐藏中访问 ControlTemplate 的部分,因为您的模板只是一个模板。这就是控件的外观。您希望您的代码隐藏总体上影响控件的行为

例如,如果您试图在某些交互情况下影响代码隐藏中的边框颜色,您确实需要添加一些(.Net4 之前的)触发器或(.Net4 之后的)一个 VisualStateManager到您的控件模板来为您管理控件的视觉状态。

You may want to reconsider your approach. What are you trying to do?

Generally, you shouldn't want or need to access portions of the ControlTemplate from your codebehind because your template is just that-- a template. It's how the control looks. You want your codebehind to generally affect the behavior of the control.

For example, if you're trying to affect the color of the border in the codebehind in certain interactive situations, you really want to add some (pre .Net4) triggers or (post .Net4) a VisualStateManager to your control template to manage your control's visual states for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文