Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
4.20/5 (5 votes)
Hi All

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...

XML
<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.
Posted
Updated 9-May-11 22:47pm
v4
Comments
Manfred Rudolf Bihy 6-May-11 8:43am    
Edit: Adjusted pre tags.
Sergey Alexandrovich Kryukov 6-May-11 8:45am    
Tag it! Platform, language, UI library. Would would want to guess?
--SA

An easier way to do this is to import System.Windows.Controls.Primitives and use the VisualTreeHelper Extensions GetVisualDescendants or GetVisualAncestors!

e.g.:

C#
protected override OnApplyTemplate()
{
    base.OnApplyTemplate();
    Border bor = GetVisualDescendants()
    .OfType<border>()
    .FirstOrDefault(x => (x as FrameworkElement).Name == "content");
};
 
Share this answer
 
v2
Apply below attribute to your ExtComboBox
[TemplatePart(Name = "FocusVisualElement", Type = typeof(Rectangle))]

Get a hold on Rectangle.

MIDL
public override void OnApplyTemplate()
         {
             base.OnApplyTemplate();
             Rect = GetTemplateChild("FocusVisualElement") as Rectangle;
         }

         public Rectangle Rect { get; private set; }


Make sure you call ApplyTemplate()

     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;
        cmbTest.ApplyTemplate();
        Rectangle rect = cmbTest.Rect;
    }
}


I hope this helps.
 
Share this answer
 
v2
Comments
Ewert Bergh 10-May-11 5:52am    
Thank you Manish, I had something similar, tried to make GetTemplateChild public on it's own but that wouldn't work...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900