WPF中ListBox的样式设置

设置之后的效果为

 

1 窗体中代码

<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="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>

2 样式文件中代码

<!--设置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>

 

转载于:https://www.cnblogs.com/zhaolili/p/4744864.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值