代码之家  ›  专栏  ›  技术社区  ›  Ievgen

Silverlight 4自定义控件子项样式

  •  0
  • Ievgen  · 技术社区  · 14 年前

    我有自定义控件,可以在其中显示一些项目。 在为自定义控件定义的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
    

    一切正常,除了儿童用品的款式 定义在 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,但它始终为空。

    我怎样才能得到这个样式的属性,我做错了什么?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Ievgen    14 年前

    我在代码中找到了解决方案和bug。

    我只是把setter添加到我的 <Style TargetType="local:Custom"> 定义并在setter的帮助下设置我想要的childitemstyle。