listbox动态选择模版

本文展示了如何在WPF中实现Listbox的动态选择模板,包括鼠标悬停时显示操作按钮的功能。内容包括两种模板样式:加号模板和名片模板,并提供了在页面中引用这些模板的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

献上效果图:

预期效果

1)Listbox动态选择模版

2)鼠标移上去后显示操作按钮


模版实现

(FButton请参考 Fbutton模版

1.加号模版

 <span style="font-size:14px;"> <ControlTemplate x:Key="AddNewTemplate" TargetType="{x:Type ListBoxItem}">
        <Border x:Name="Border"
                BorderThickness="0"
                CornerRadius="5"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
            <Border.Background>
                <ImageBrush ImageSource="/Management;component/Images/mohmoo/bg_card.png" />
            </Border.Background>
            <!--<Grid Width="{TemplateBinding Width}"
                  Height="{TemplateBinding Height}"
                  ShowGridLines="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="30" />
                </Grid.ColumnDefinitions>-->

            <local:FButton Width="{TemplateBinding Width}"
                           Height="{TemplateBinding Height}"
                           HorizontalAlignment="Center"
                           VerticalContentAlignment="Center"
                           Background="Transparent"
                           Command="{Binding AddClickCommand}"
                           FIcon="&#xe604;"
                           FIconSize="70"
                           Foreground="#0068FD"
                           MouseOverBackground="Transparent"
                           MouseOverForeground="Red"
                           PressedBackground="Transparent"
                           Template="{StaticResource FButton_Template}" />

            <!--</Grid>-->

        </Border>
        <ControlTemplate.Triggers>

            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="Border" Property="BorderThickness" Value="2" />
                <Setter TargetName="Border" Property="BorderBrush" Value="#24A6CA" />
            </Trigger>

            <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="Border" Property="Opacity" Value="{StaticResource DisableOpacity}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate></span>

2.名片模版

<ControlTemplate x:Key="CardTemplate" TargetType="{x:Type ListBoxItem}">
        <Border x:Name="Border"
                BorderThickness="0"
                CornerRadius="5"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
            <Border.Background>
                <ImageBrush ImageSource="/Management;component/Images/mohmoo/bg_card.png" />
            </Border.Background>

            <Grid Margin="1">
                <Grid Background="Transparent">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50" />
                        <RowDefinition Height="50" />
                        <RowDefiniti
要实现WPF Listbox的鼠标悬浮效果,可以使用ListBox.ItemContainerStyle和Trigger来实现。具体步骤如下: 1. 在ListBox中添加ItemContainerStyle属性,指定ListBoxItem的样式: ``` <ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"> <ListBoxItem>Item 1</ListBoxItem> <ListBoxItem>Item 2</ListBoxItem> <ListBoxItem>Item 3</ListBoxItem> </ListBox> ``` 2. 在资源字典中定义ListBoxItemStyle样式: ``` <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Padding" Value="5" /> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border x:Name="border" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="#FFCCE5FF" /> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="border" Property="Background" Value="#FF007ACC" /> <Setter Property="Foreground" Value="White" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 3. 在ListBoxItemStyle样式中添加IsMouseOver Trigger,指定鼠标悬浮的背景色。 4. 可以根据需要,添加IsSelected Trigger,指定选中样式。 这样就可以实现WPF Listbox的鼠标悬浮效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值