获取 ContentControl 控件的子模板?

发布于 2024-11-05 15:06:04 字数 2142 浏览 3 评论 0原文

我们正在开发一个使用通用自定义 ContentControl 的 Silverlight 应用程序。此 ContentControl 具有在 Generic.xaml 中指定的控件模板。

继承的 ContentControl 的模板...

<Style TargetType="local:ExtContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ExtContentControl">
                <Border x:Name="content" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Child="{TemplateBinding Content}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

继承的 ComboBox 的模板...

<controltemplate targettype="local:ExtComboBox"></controltemplate>

...

<Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Margin="1" Opacity="0" RadiusY="2" RadiusX="2" Stroke="#FF6DBDD1" StrokeThickness="1"/>

当实例化时,ContentControl 的内容被设置为一个(通用)控件,可以是文本框、下拉列表、标签或日期选择器。

public class ExtContentControl : ContentControl
{
    public ExtContentControl()
    {
        this.DefaultStyleKey = typeof(ExtContentControl);

        RenderControl();
    }

    private void RenderControl()
    {
        ExtComboBox extComboBox = new ExtComboBox();
        this.Content = extComboBox;
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        Border bor = GetTemplateChild("content") as Border;

        ExtComboBox cmbTest = bor.Child as ExtComboBox;

        //Find FocusVisualElement from ExtComboBox Control Template
        //Rectangle rec = cmbTest.FindName("FocusVisualElement") as Rectangle;
        //cmbTest returns null
    }
}

正如您在我的上一条评论中看到的...

//从 ExtComboBox 控件模板中查找 FocusVisualElement //矩形rec = cmbTest.FindName("FocusVisualElement") as 矩形; //cmbTest 返回 null

如何从 ContentControl 内的 OnApplyTemplate 内部获取 FocusVisualElement?

希望这是有道理的。

We're working on a Silverlight application that uses a generic custom ContentControl. This ContentControl has a Control Template specified in a Generic.xaml.

The inherited ContentControl's Template...

<Style TargetType="local:ExtContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ExtContentControl">
                <Border x:Name="content" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Child="{TemplateBinding Content}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The inherited ComboBox's Template...

<controltemplate targettype="local:ExtComboBox"></controltemplate>

...

<Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Margin="1" Opacity="0" RadiusY="2" RadiusX="2" Stroke="#FF6DBDD1" StrokeThickness="1"/>

When it is instantiated the ContentControl's content is set to a (generic) control which can be a Textbox, Dropdown, Label or Datepicker.

public class ExtContentControl : ContentControl
{
    public ExtContentControl()
    {
        this.DefaultStyleKey = typeof(ExtContentControl);

        RenderControl();
    }

    private void RenderControl()
    {
        ExtComboBox extComboBox = new ExtComboBox();
        this.Content = extComboBox;
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        Border bor = GetTemplateChild("content") as Border;

        ExtComboBox cmbTest = bor.Child as ExtComboBox;

        //Find FocusVisualElement from ExtComboBox Control Template
        //Rectangle rec = cmbTest.FindName("FocusVisualElement") as Rectangle;
        //cmbTest returns null
    }
}

As you can see in my last comment...

//Find FocusVisualElement from ExtComboBox Control Template
//Rectangle rec = cmbTest.FindName("FocusVisualElement") as Rectangle;
//cmbTest returns null

How can I get hold of FocusVisualElement from inside OnApplyTemplate inside the ContentControl?

Hope this makes sense.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文