Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF内容控件样式_C#_Wpf_Styles - Fatal编程技术网

C# WPF内容控件样式

C# WPF内容控件样式,c#,wpf,styles,C#,Wpf,Styles,我有一个自定义控件,它基本上是一个contentcontrol public class PromoAlarmBox : ContentControl { static PromoAlarmBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(PromoAlarmBox), new FrameworkPropertyMetadata(typeof(PromoAlarmBox

我有一个自定义控件,它基本上是一个contentcontrol

 public class PromoAlarmBox : ContentControl
    {
        static PromoAlarmBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PromoAlarmBox), new FrameworkPropertyMetadata(typeof(PromoAlarmBox)));
        }
    }
我将它添加到包含它的用户控件中

<controls:PromoAlarmBox Grid.Row="9" Grid.Column="1"  />

如果我将样式添加到包含usercontrols的资源中,一切都会正常工作

 <UserControl.Resources>
        <Style  TargetType="{x:Type controls:PromoAlarmBox}">
            <Setter Property="ContentControl.ContentTemplate">
                <Setter.Value>
                    <DataTemplate >
                        <Rectangle Fill="Blue" Stroke="Black" Height="20" Width="20"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

但如果我将其添加到custom controls项目中的generic.xaml,则不会显示任何内容

<Style  TargetType="{x:Type local:PromoAlarmBox}">
        <Setter Property="ContentControl.ContentTemplate">
            <Setter.Value>
                <DataTemplate >
                    <Rectangle Fill="Blue" Stroke="Black" Height="20" Width="20"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>


我知道该样式是应用的,因为我在同一个项目中有其他控件,这些控件的样式是在generic.xaml中定义的,有人有什么想法吗?

一个简单的
静态
就可以了

 static PromoAlarmBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(PromoAlarmBox), new FrameworkPropertyMetadata(typeof(PromoAlarmBox)));
    }

虽然我不知道为什么当你使用一个样式作为本地资源和使用泛型时会有区别,但这对我来说是可行的

<Style TargetType="{x:Type local:PromoAlarmBox}">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Rectangle VerticalAlignment="Stretch" Fill="Yellow" Stroke="Black" Height="20" Width="20"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

尝试过,但没有成功