我们正在开发一个使用通用自定义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的内容设置为(通用)控件,可以是TextBox,Dropdown,Label或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 } }
//从ExtComboBox控件模板中查找FocusVisualElement
// Rectangle rec = cmbTest.FindName(“FocusVisualElement”)为Rectangle;
// cmbTest返回null
如何从ContentControl内的OnApplyTemplate内部获取FocusVisualElement?
希望这是有道理的.