4

我们正在开发一个使用通用自定义 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 //Rectangle rec = cmbTest.FindName("FocusVisualElement") as Rectangle; //cmbTest 返回 null

如何从 ContentControl 中的 OnApplyTemplate 中获取 FocusVisualElement?

希望这是有道理的。

4

1 回答 1

1

解决这个...

http://www.codeproject.com/Questions/192431/Hover-Foreground-Colour-with-dynamic-binding-Conte.aspx

于 2011-05-10T09:54:07.290 回答