Style settings for a listbox in WPF

Source: Internet
Author: User

The effect after setting is

1 code in the form

<window x:class= "Qynodetest.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Title= "MainWindow" height= "width=" 525 ">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<resourcedictionary source= "Style/style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<listbox style= "{StaticResource listboxhor}" >
<ListBoxItem>
<Border>
<StackPanel>
<textblock horizontalalignment= "Center" > Project 1</textblock>
<button content= "OK" width= "horizontalalignment=" Center/>
</StackPanel>
</Border>
</ListBoxItem>
<ListBoxItem>
<Border>
<StackPanel>
<textblock horizontalalignment= "Center" > Project 1</textblock>
<button content= "OK" width= "horizontalalignment=" Center/>
</StackPanel>
</Border>
</ListBoxItem>
<ListBoxItem>
<Border>
<StackPanel>
<textblock horizontalalignment= "Center" > Project 1</textblock>
<button content= "OK" width= "horizontalalignment=" Center/>
</StackPanel>
</Border>
</ListBoxItem>
<ListBoxItem>
<Border>
<StackPanel>
<textblock horizontalalignment= "Center" > Project 1</textblock>
<button content= "OK" width= "horizontalalignment=" Center/>
</StackPanel>
</Border>
</ListBoxItem>
</ListBox>
</Grid>
</Window>

2 code in style files

<!--set the listbox style--

<style targettype= "ListBox" x:key= "Listboxhor" >

<!--setting up a template--
<setter property= "Template" >
<Setter.Value>
<controltemplate targettype= "ListBox" >
<scrollviewer horizontalscrollbarvisibility= "Disabled" verticalscrollbarvisibility= "Auto" >
<wrappanel orientation= "Horizontal" isitemshost= "true" scrollviewer.cancontentscroll= "true"/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


<!--set ListBoxItem style --
<style targettype= "ListBoxItem" >
<setter property= "Width" value= "></Setter>"
<setter property= "Height" value= "></Setter>"
<setter property= "Margin" value= "5" ></Setter>
<setter property= "BorderBrush" value= "Red"/>
<setter property= "BorderThickness" value= "1"/>
<!--set the control template--
<setter property= "Template" >
<Setter.Value>
<controltemplate targettype= "ListBoxItem" >
<border background= "{TemplateBinding Background}" borderbrush= "{TemplateBinding BorderBrush}" borderthickness= "{ TemplateBinding borderthickness} ">
<contentpresenter horizontalalignment= "Center" verticalalignment= "{TemplateBinding verticalcontentalignment}" Textblock.foreground= "{TemplateBinding Foreground}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<!--setting Triggers--
<Style.Triggers>
<trigger property= "IsSelected" value= "true" >
<setter property= "Background" value= "#808080"/>
<setter property= "Foreground" value= "white"/>
<setter property= "BorderBrush" value= "Green"/>
<setter property= "BorderThickness" value= "2"/>
</Trigger>
<trigger property= "IsMouseOver" value= "true" >
<setter property= "Background" value= "Red"/>
<setter property= "BorderBrush" value= "Black"/>
<setter property= "BorderThickness" value= "2"/>
</Trigger>
</Style.Triggers>
</Style>

Style settings for a listbox in WPF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.