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);