I have a custom control inheriting from ListBox
.
I have a style for it targeting my custom control.
For some reason this style is not working on my custom control.
Could you please tell me what I am missing here?
Code for style:
<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>
Code for custom control:
public class CustomListBox : ListBox
{
public CustomListBox()
{
this.DefaultStyleKey = typeof(CustomListBox).Name;
}
}
Usage of custom control:
<local:CustomListBox>
<ListBoxItem Content="AAA"></ListBoxItem>
<ListBoxItem Content="BBB"></ListBoxItem>
<ListBoxItem Content="CCC"></ListBoxItem>
</local:CustomListBox>
Any help?
Thank you!
In Windows Phone 8.1 when you create a Templated Control (Custom control) a file is automatically created called Generic.xaml under Themes folder. Your style should be added in Generic.xaml like this.
<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>
Change the following line to
this.DefaultStyleKey = typeof(CustomListBox);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.