【问题标题】:Silverlight 4 custom control child items styleSilverlight 4 自定义控件子项样式
【发布时间】: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


【解决方案1】:

我在我的代码中找到了解决方案和错误。

我只是将 setter 添加到我的 &lt;Style TargetType="local:Custom"&gt; 定义中,并在我想要的 setter set ChildItemStyle 的帮助下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2023-04-04
    相关资源
    最近更新 更多