问题描述
大家好
我们正在开发一个使用通用自定义ContentControl的Silverlight应用程序.此ContentControl具有在Generic.xaml中指定的控制模板.
继承的ContentControl的模板...
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...
<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的模板...
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"/>
实例化时,ContentControl的内容将设置为(通用)控件,该控件可以是Textbox,Dropdown,Label或Datepicker.
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
}
}
如您在我的最后评论中所见...
//从ExtComboBox控件模板中找到FocusVisualElement
//Rectangle rec = cmbTest.FindName("FocusVisualElement")as Rectangle;
//cmbTest返回null
如何从ContentControl的OnApplyTemplate内部获取FocusVisualElement?
希望这是有道理的.
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.
推荐答案
protected override OnApplyTemplate()
{
base.OnApplyTemplate();
Border bor = GetVisualDescendants()
.OfType<border>()
.FirstOrDefault(x => (x as FrameworkElement).Name == "content");
};
[TemplatePart(Name = "FocusVisualElement", Type = typeof(Rectangle))]
抓住Rectangle.
Get a hold on Rectangle.
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Rect = GetTemplateChild("FocusVisualElement") as Rectangle;
}
public Rectangle Rect { get; private set; }
确保您调用ApplyTemplate()
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.
这篇关于获取ContentControl控件的模板子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!