【发布时间】:2010-10-12 18:24:37
【问题描述】:
我有自定义控件,我想在其中显示一些项目。 在 generic.xaml 中为自定义控件定义样式:
<Style TargetType="local:Custom">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:Customl">
<!-- Root Grid-->
<Grid x:Name="CustomLayout"
Background="Black">
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我设置为 C#
[TemplatePart(Name = "CustomLayout", Type = typeof(Grid))]
[StyleTypedProperty(Property = "ChildItemStyle", StyleTargetType = typeof(Control))]
public class GaugeControl : Control
除了子项的样式外,一切正常 定义在 generic.xaml:
<Style TargetType="{x:Type Control}">
<Setter Property="Background" Value="Red"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<!-- Root Grid-->
<Grid x:Name="LayoutRoot"
Background="Red">
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
带有属性的自定义控件
[StyleTypedProperty(Property = "ChildItemStyle", StyleTargetType = typeof(Control))]
我的自定义控件中有 ChildItemStyle DependencyProperty,但它始终为空。
我怎样才能得到这个 Style 属性,我在做什么错?
【问题讨论】:
-
你用的是silverlight,对吧?您是否尝试过 TargetType="Control" 而不是 x:Type...?
-
是的,我试过了,但错误是我在 generic.xaml 中有单独的样式,为本地设置了一种样式:使用 setter 将样式设置为 ChildItemStyle 的自定义
标签: c# .net silverlight