WPF中ListBox的样式设置设置之后的效果为
1 窗体中代码
<Window x:Class="QyNodeTest.MainWindow"
xmlns="schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="schemas.microsoft/winfx/2006/xaml"
Title="MainWindow" Height="350" 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">项⽬1</TextBlock>
<Button Content="OK" Width="80" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</ListBoxItem>
<ListBoxItem>
<Border>
<StackPanel>
<TextBlock HorizontalAlignment="Center">项⽬1</TextBlock>
<Button Content="OK" Width="80" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</ListBoxItem>
<ListBoxItem>
<Border>
<StackPanel>
<TextBlock HorizontalAlignment="Center">项⽬1</TextBlock>
<Button Content="OK" Width="80" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</ListBoxItem>
<ListBoxItem>
<Border>
<StackPanel>
<TextBlock HorizontalAlignment="Center">项⽬1</TextBlock>
<Button Content="OK" Width="80" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</ListBoxItem>
</ListBox>
</Grid>
</Window>
borderbox2 样式⽂件中代码
<!--设置ListBox样式-->
<Style TargetType="ListBox" x:Key="ListBoxHor">
    <!--设置模板-->
<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>
<!--设置ListBoxItem样式-->
<Style TargetType="ListBoxItem">
<Setter Property="Width" Value="120"></Setter>
<Setter Property="Height" Value="40"></Setter>
<Setter Property="Margin" Value="5"></Setter>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="1"/>
<!--设置控件模板-->
<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>
<!--设置触发器-->
<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>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。