Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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# 如何将样式应用于自定义控件_C#_Xaml_Wpf Controls - Fatal编程技术网

C# 如何将样式应用于自定义控件

C# 如何将样式应用于自定义控件,c#,xaml,wpf-controls,C#,Xaml,Wpf Controls,我有一个从列表框继承的自定义控件 我有一个针对自定义控件的样式 由于某些原因,此样式在我的自定义控件上不起作用 你能告诉我这里缺少什么吗 样式代码: <Style x:Key="ListBoxStyle" TargetType="local:CustomListBox"> <Setter Property="Background" Value="Red"/> <Setter Property="ItemsPanel"> <

我有一个从
列表框继承的自定义控件

我有一个针对自定义控件的样式

由于某些原因,此样式在我的自定义控件上不起作用

你能告诉我这里缺少什么吗

样式代码:

<Style x:Key="ListBoxStyle" TargetType="local:CustomListBox">
    <Setter Property="Background" Value="Red"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>
自定义控件的用法:

public class CustomListBox : ListBox
{
    public CustomListBox()
    {
        this.DefaultStyleKey = typeof(CustomListBox).Name;
    }

}
<local:CustomListBox>
        <ListBoxItem Content="AAA"></ListBoxItem>
        <ListBoxItem Content="BBB"></ListBoxItem>
        <ListBoxItem Content="CCC"></ListBoxItem>
    </local:CustomListBox>

有什么帮助吗


谢谢大家!

在Windows Phone 8.1中,创建模板控件(自定义控件)时,会在主题文件夹下自动创建名为Generic.xaml的文件。 您的样式应该像这样添加到Generic.xaml中

<Style TargetType="local:CustomListBox">
    <Setter Property="Background" Value="Red"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter> 
</Style>

这是Windows 8/8.1还是WPF?
this.DefaultStyleKey = typeof(CustomListBox);