获取 ContentControl 控件的子模板?
我们正在开发一个使用通用自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案...
http:// /www.codeproject.com/Questions/192431/Hover-Foreground-Colour-with-dynamic-binding-Conte.aspx
Solution for this...
http://www.codeproject.com/Questions/192431/Hover-Foreground-Colour-with-dynamic-binding-Conte.aspx