如何在模板中使用控件? [英] how can i use a control in template?

查看:22
本文介绍了如何在模板中使用控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Style x:Key="abc" TargetType="{x:Type Window}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <button x:name="btn">my button!!</button>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
...
<Window ... Style="{StaticResource styleMainWindow}">

我如何使用btn按钮?

达到您的期望,

MessageBox.Show(this.btn1.name);

在编译时发生错误.并且 btn1 也没有出现在智能感知中.

was occured an error at compile time. and also btn1 didn't show up in intelisense.

推荐答案

尝试 FindName 方法.

假设this是您控件的上下文:

Assuming this is your Control's context:

var button = (Button)this.Template.FindName("btn", this);

这篇关于如何在模板中使用控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆